001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.tools.ant;
012:
013: import org.apache.tools.ant.BuildException;
014: import org.apache.tools.ant.AntClassLoader;
015: import org.apache.tools.ant.types.Path;
016: import org.apache.tools.ant.types.Reference;
017: import org.apache.tools.ant.taskdefs.MatchingTask;
018:
019: import com.versant.core.metadata.ModelMetaData;
020: import com.versant.core.logging.LogEventStore;
021: import com.versant.core.common.config.ConfigParser;
022: import com.versant.core.common.config.ConfigInfo;
023: import com.versant.core.util.classhelper.ClassHelper;
024: import com.versant.core.storagemanager.StorageManagerFactory;
025: import com.versant.core.storagemanager.StorageManagerFactoryBuilder;
026:
027: /**
028: * Base class for tasks that need jdo meta data.
029: */
030: public class JdoTaskBase
031:
032: extends MatchingTask {
033:
034: public static String DEFAULT_CONFIG_FILENAME = "versant.properties";
035: protected String configFilename = DEFAULT_CONFIG_FILENAME;
036:
037: protected ModelMetaData metaData;
038: protected LogEventStore pes;
039: protected StorageManagerFactory smf;
040: protected StorageManagerFactory innermostSmf;
041: protected ClassLoader taskClassLoader;
042:
043: protected Path classpath;
044:
045: public void setConfig(String config) {
046: configFilename = config;
047: }
048:
049: public String getConfig() {
050: return configFilename;
051: }
052:
053: public void setProject(String config) {
054: setConfig(config);
055: }
056:
057: public void log(String msg) {
058: if (project == null) {
059: System.out.println(msg);
060: }
061:
062: else {
063: super .log(msg);
064: }
065:
066: }
067:
068: /**
069: * Set the classpath to be used for this compilation.
070: */
071:
072: public void setClasspath(Path classpath) {
073: if (classpath == null) {
074: this .classpath = classpath;
075: } else {
076: classpath.append(classpath);
077: }
078: }
079:
080: protected ClassLoader getClassLoader() {
081:
082: if (taskClassLoader != null)
083: return taskClassLoader;
084: ClassLoader cl = getClass().getClassLoader();
085: if (cl == null) {
086: cl = ClassHelper.get().getSystemClassLoader();
087: }
088: if (classpath == null || project == null) {
089: taskClassLoader = cl;
090: } else {
091: taskClassLoader = new AntClassLoader(cl, project,
092: classpath, true);
093: }
094: return taskClassLoader;
095:
096: }
097:
098: /**
099: * Creates a nested classpath element.
100: */
101:
102: public Path createClasspath() {
103: if (classpath == null) {
104: classpath = new Path(project);
105: }
106: return classpath.createPath();
107: }
108:
109: /**
110: * Adds a reference to a CLASSPATH defined elsewhere.
111: */
112:
113: public void setClasspathRef(Reference r) {
114: createClasspath().setRefid(r);
115: }
116:
117: /**
118: * Gets the classpath.
119: */
120:
121: public Path getClasspath() {
122: return classpath;
123: }
124:
125: /**
126: * Initialize this task. This will load all the meta data and create
127: * and initialize data stores etc.
128: */
129: public void execute() {
130: // parse the config
131: ConfigParser p = new ConfigParser();
132:
133: Thread.currentThread().setContextClassLoader(getClassLoader());
134: ConfigInfo config = p.parseResource(configFilename,
135: getClassLoader());
136: config.validate();
137:
138: /*
139: // create metadata from roots AND names
140: MetaDataParser mp = new MetaDataParser();
141: JdoRoot[] extra = mp.parse(config.jdoResources, loader);
142: JdoRoot[] combined = new JdoRoot[extra.length +
143: config.jdoMetaData.length];
144: System.arraycopy(config.jdoMetaData, 0, combined, 0,
145: config.jdoMetaData.length);
146: System.arraycopy(extra, 0, combined, config.jdoMetaData.length,
147: extra.length);
148: config.jdoMetaData = combined;
149: */
150:
151: StorageManagerFactoryBuilder b = new StorageManagerFactoryBuilder();
152: b.setConfig(config);
153:
154: b.setLoader(getClassLoader());
155:
156: b.setOnlyMetaData(!isCreateConnectionPool());
157: b.setFullInit(false);
158: smf = b.createStorageManagerFactory();
159: for (innermostSmf = smf;;) {
160: StorageManagerFactory next = innermostSmf
161: .getInnerStorageManagerFactory();
162: if (next == null)
163: break;
164: innermostSmf = next;
165: }
166: pes = b.getLogEventStore();
167: }
168:
169: /**
170: * Override this to return false if a task does not require actual
171: * datastore connections.
172: */
173: protected boolean isCreateConnectionPool() {
174: return true;
175: }
176:
177: /**
178: * Initialize the data stores. This must be called if the stores are
179: * to be used for anything other than meta data generation.
180: */
181: protected void initDataStores() {
182: //dataStore.init();
183: }
184:
185: public ModelMetaData getMetaData() {
186: return metaData;
187: }
188:
189: public StorageManagerFactory getSmf() {
190: return smf;
191: }
192:
193: public StorageManagerFactory getInnermostSmf() {
194: return innermostSmf;
195: }
196:
197: public LogEventStore getPerfEventStore() {
198: return pes;
199: }
200:
201: public void throwBuildException(String str) {
202: throwBuildException(str, null);
203: }
204:
205: public void throwBuildException(String str, Throwable e) {
206:
207: throw new BuildException(str, e);
208:
209: }
210: }
|