001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.conf;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.openjpa.kernel.Bootstrap;
026: import org.apache.openjpa.kernel.Broker;
027: import org.apache.openjpa.kernel.BrokerFactory;
028: import org.apache.openjpa.kernel.Query;
029: import org.apache.openjpa.lib.util.Options;
030: import org.apache.openjpa.lib.log.Log;
031: import org.apache.openjpa.meta.ClassMetaData;
032: import org.apache.openjpa.meta.MetaDataRepository;
033: import org.apache.openjpa.meta.QueryMetaData;
034: import org.apache.openjpa.meta.SequenceMetaData;
035:
036: /**
037: * Performs maintenance tasks on the metadata caches accessible via the
038: * {@link CacheMarshaller} architecture.
039: *
040: * @since 1.1.0
041: */
042: public class MetaDataCacheMaintenance {
043:
044: private final BrokerFactory factory;
045: private final OpenJPAConfiguration conf;
046: private final boolean devpath;
047: private Log log;
048:
049: public static void main(String[] args) {
050: Options opts = new Options();
051: args = opts.setFromCmdLine(args);
052: boolean devpath = opts.getBooleanProperty("scanDevPath",
053: "ScanDevPath", true);
054:
055: BrokerFactory factory = Bootstrap.newBrokerFactory();
056: try {
057: MetaDataCacheMaintenance maint = new MetaDataCacheMaintenance(
058: factory, devpath);
059:
060: if (args.length != 1)
061: usage();
062:
063: if ("store".equals(args[0]))
064: maint.store();
065: else if ("dump".equals(args[0]))
066: maint.dump();
067: else
068: usage();
069: } finally {
070: factory.close();
071: }
072: }
073:
074: /**
075: * @deprecated logging is routed to the logging system now.
076: */
077: public MetaDataCacheMaintenance(BrokerFactory factory,
078: boolean devpath, boolean verbose) {
079: this (factory, devpath);
080: }
081:
082: /**
083: * @param factory The {@link BrokerFactory} for which cached metadata
084: * should be built.
085: * @param devpath Whether or not to scan the development environment paths
086: * to find persistent types to store.
087: */
088: public MetaDataCacheMaintenance(BrokerFactory factory,
089: boolean devpath) {
090: this .factory = factory;
091: this .conf = factory.getConfiguration();
092: this .devpath = devpath;
093: this .log = conf.getLog(OpenJPAConfiguration.LOG_TOOL);
094: }
095:
096: public void setLog(Log log) {
097: this .log = log;
098: }
099:
100: private static int usage() {
101: System.err.println("Usage: java MetaDataCacheMaintenance "
102: + "[-scanDevPath t|f] store | dump");
103: return -1;
104: }
105:
106: /**
107: * The metadata repository for the factory that this instance was
108: * constructed with will be serialized, along with any query
109: * compilations etc. that have been created for the factory.
110: */
111: public void store() {
112: MetaDataRepository repos = conf.getMetaDataRepositoryInstance();
113: repos.setSourceMode(MetaDataRepository.MODE_ALL);
114: Collection types = repos.loadPersistentTypes(devpath, null);
115: for (Iterator iter = types.iterator(); iter.hasNext();)
116: repos.getMetaData((Class) iter.next(), null, true);
117:
118: loadQueries();
119:
120: log.info("The following data will be stored: ");
121: log(repos, conf.getQueryCompilationCacheInstance());
122:
123: CacheMarshallersValue.getMarshallerById(conf,
124: getClass().getName()).store(
125: new Object[] { repos,
126: conf.getQueryCompilationCacheInstance() });
127: }
128:
129: private void loadQueries() {
130: Broker broker = factory.newBroker();
131: try {
132: QueryMetaData[] qmds = conf.getMetaDataRepositoryInstance()
133: .getQueryMetaDatas();
134: for (int i = 0; i < qmds.length; i++)
135: loadQuery(broker, qmds[i]);
136: } finally {
137: broker.close();
138: }
139: }
140:
141: private void loadQuery(Broker broker, QueryMetaData qmd) {
142: try {
143: Query q = broker.newQuery(qmd.getLanguage(), null);
144: qmd.setInto(q);
145: q.compile();
146: } catch (Exception e) {
147: if (log.isTraceEnabled()) {
148: log.warn("Skipping named query " + qmd.getName() + ": "
149: + e.getMessage(), e);
150: } else {
151: log.warn("Skipping named query " + qmd.getName() + ": "
152: + e.getMessage());
153: }
154: }
155: }
156:
157: public void dump() {
158: Object[] os = (Object[]) CacheMarshallersValue
159: .getMarshallerById(conf, getClass().getName()).load();
160: if (os == null) {
161: log.info("No cached data was found");
162: return;
163: }
164: MetaDataRepository repos = (MetaDataRepository) os[0];
165: Map qcc = (Map) os[1];
166:
167: log.info("The following data was found: ");
168: log(repos, qcc);
169: }
170:
171: private void log(MetaDataRepository repos, Map qcc) {
172: ClassMetaData[] metas = repos.getMetaDatas();
173: log.info(" Types: " + metas.length);
174: if (log.isTraceEnabled())
175: for (int i = 0; i < metas.length; i++)
176: log.trace(" "
177: + metas[i].getDescribedType().getName());
178:
179: QueryMetaData[] qmds = repos.getQueryMetaDatas();
180: log.info(" Queries: " + qmds.length);
181: if (log.isTraceEnabled())
182: for (int i = 0; i < qmds.length; i++)
183: log.trace(" " + qmds[i].getName() + ": "
184: + qmds[i].getQueryString());
185:
186: SequenceMetaData[] smds = repos.getSequenceMetaDatas();
187: log.info(" Sequences: " + smds.length);
188: if (log.isTraceEnabled())
189: for (int i = 0; i < smds.length; i++)
190: log.trace(" " + smds[i].getName());
191:
192: log.info(" Compiled queries: "
193: + (qcc == null ? "0" : "" + qcc.size()));
194: if (log.isTraceEnabled() && qcc != null)
195: for (Iterator iter = qcc.keySet().iterator(); iter
196: .hasNext();)
197: log.trace(" " + iter.next());
198: }
199: }
|