KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > ant > JdoTaskBase


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.ant;
13
14
15
16 import org.apache.tools.ant.BuildException;
17 import org.apache.tools.ant.AntClassLoader;
18 import org.apache.tools.ant.types.Path;
19 import org.apache.tools.ant.types.Reference;
20 import org.apache.tools.ant.taskdefs.MatchingTask;
21
22
23 import com.versant.core.metadata.ModelMetaData;
24 import com.versant.core.logging.LogEventStore;
25 import com.versant.core.common.config.ConfigParser;
26 import com.versant.core.common.config.ConfigInfo;
27 import com.versant.core.util.classhelper.ClassHelper;
28 import com.versant.core.storagemanager.StorageManagerFactory;
29 import com.versant.core.storagemanager.StorageManagerFactoryBuilder;
30
31
32
33 /**
34  * Base class for tasks that need jdo meta data.
35  */

36 public class JdoTaskBase
37
38         extends MatchingTask
39  {
40
41     public static String JavaDoc DEFAULT_CONFIG_FILENAME = "versant.properties";
42     protected String JavaDoc configFilename = DEFAULT_CONFIG_FILENAME;
43
44     protected ModelMetaData metaData;
45     protected LogEventStore pes;
46     protected StorageManagerFactory smf;
47     protected StorageManagerFactory innermostSmf;
48     protected ClassLoader JavaDoc taskClassLoader;
49
50
51     protected Path classpath;
52
53
54
55     public void setConfig(String JavaDoc config) {
56         configFilename = config;
57     }
58
59     public String JavaDoc getConfig() {
60         return configFilename;
61     }
62
63     public void setProject(String JavaDoc config) {
64         setConfig(config);
65     }
66
67     public void log(String JavaDoc msg) {
68         if (project == null) {
69             System.out.println(msg);
70         }
71
72         else {
73             super.log(msg);
74         }
75
76     }
77
78     /**
79      * Set the classpath to be used for this compilation.
80      */

81
82     public void setClasspath(Path classpath) {
83         if (classpath == null) {
84             this.classpath = classpath;
85         } else {
86             classpath.append(classpath);
87         }
88     }
89
90
91
92     protected ClassLoader JavaDoc getClassLoader() {
93
94         if (taskClassLoader != null) return taskClassLoader;
95         ClassLoader JavaDoc cl = getClass().getClassLoader();
96         if (cl == null) {
97             cl = ClassHelper.get().getSystemClassLoader();
98         }
99         if (classpath == null || project == null) {
100             taskClassLoader = cl;
101         } else {
102             taskClassLoader = new AntClassLoader(cl, project, classpath, true);
103         }
104         return taskClassLoader;
105
106
107     }
108
109     /**
110      * Creates a nested classpath element.
111      */

112
113     public Path createClasspath() {
114         if (classpath == null) {
115             classpath = new Path(project);
116         }
117         return classpath.createPath();
118     }
119
120
121     /**
122      * Adds a reference to a CLASSPATH defined elsewhere.
123      */

124
125     public void setClasspathRef(Reference r) {
126         createClasspath().setRefid(r);
127     }
128
129
130     /**
131      * Gets the classpath.
132      */

133
134     public Path getClasspath() {
135         return classpath;
136     }
137
138
139
140
141     /**
142      * Initialize this task. This will load all the meta data and create
143      * and initialize data stores etc.
144      */

145     public void execute()
146     {
147         // parse the config
148
ConfigParser p = new ConfigParser();
149
150         Thread.currentThread().setContextClassLoader(getClassLoader());
151         ConfigInfo config = p.parseResource(configFilename, getClassLoader());
152         config.validate();
153
154
155
156 /*
157         // create metadata from roots AND names
158         MetaDataParser mp = new MetaDataParser();
159         JdoRoot[] extra = mp.parse(config.jdoResources, loader);
160         JdoRoot[] combined = new JdoRoot[extra.length +
161                 config.jdoMetaData.length];
162         System.arraycopy(config.jdoMetaData, 0, combined, 0,
163                 config.jdoMetaData.length);
164         System.arraycopy(extra, 0, combined, config.jdoMetaData.length,
165                 extra.length);
166         config.jdoMetaData = combined;
167 */

168
169         StorageManagerFactoryBuilder b = new StorageManagerFactoryBuilder();
170         b.setConfig(config);
171
172         b.setLoader(getClassLoader());
173
174         b.setOnlyMetaData(!isCreateConnectionPool());
175         b.setFullInit(false);
176         smf = b.createStorageManagerFactory();
177         for (innermostSmf = smf; ;) {
178             StorageManagerFactory next =
179                     innermostSmf.getInnerStorageManagerFactory();
180             if (next == null) break;
181             innermostSmf = next;
182         }
183         pes = b.getLogEventStore();
184     }
185
186     /**
187      * Override this to return false if a task does not require actual
188      * datastore connections.
189      */

190     protected boolean isCreateConnectionPool() {
191         return true;
192     }
193
194     /**
195      * Initialize the data stores. This must be called if the stores are
196      * to be used for anything other than meta data generation.
197      */

198     protected void initDataStores() {
199         //dataStore.init();
200
}
201
202     public ModelMetaData getMetaData() {
203         return metaData;
204     }
205
206     public StorageManagerFactory getSmf() {
207         return smf;
208     }
209
210     public StorageManagerFactory getInnermostSmf() {
211         return innermostSmf;
212     }
213
214     public LogEventStore getPerfEventStore() {
215         return pes;
216     }
217
218     public void throwBuildException(String JavaDoc str) {
219         throwBuildException(str, null);
220     }
221
222     public void throwBuildException(String JavaDoc str, Throwable JavaDoc e) {
223         
224         throw new BuildException(str, e);
225         
226
227     }
228 }
229
Popular Tags