KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teatools > PackageDescriptor


1 /* ====================================================================
2  * TeaTools - Copyright (c) 1997-2000 GO.com
3  * ====================================================================
4  * The Tea Software License, Version 1.0
5  *
6  * Copyright (c) 2000 GO.com. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by GO.com
23  * (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "GO.com" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@go.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle" or "Trove" appear in their name, without prior written
35  * permission of GO.com.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL GO.COM OR ITS CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
47  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.teatools;
54
55 import java.lang.reflect.Method JavaDoc;
56 import java.util.Date JavaDoc;
57
58 /******************************************************************************
59  * Wrapper for information retrieved from a package's <code>PackageInfo</code>
60  * class <i>or</i> <code>java.lang.Package</code> object.
61  *
62  * @author Mark Masse
63  * @version
64  * <!--$$Revision:--> 1 <!-- $-->, <!--$$JustDate:--> 2/20/01 <!-- $-->
65  */

66 public class PackageDescriptor {
67
68     /**
69      * Creates a PackageDescriptor for the named package.
70      */

71     public static PackageDescriptor forName(String JavaDoc packageName) {
72         return forName(packageName, null);
73     }
74
75     /**
76      * Creates a PackageDescriptor for the named package using the
77      * specified ClassLoader to load the PackageInfo or Package.
78      */

79     public static PackageDescriptor forName(String JavaDoc packageName,
80                                             ClassLoader JavaDoc classLoader) {
81         
82         PackageDescriptor pd = new PackageDescriptor(packageName);
83         if (classLoader == null) {
84             classLoader = ClassLoader.getSystemClassLoader();
85         }
86         
87         if (packageName == null || packageName.trim().length() == 0) {
88             return pd;
89         }
90         
91         String JavaDoc packageInfoClassName = packageName;
92
93         if (!packageInfoClassName.endsWith(".")) {
94             packageInfoClassName = packageInfoClassName + ".";
95         }
96         
97         packageInfoClassName = packageInfoClassName + "PackageInfo";
98
99
100         Class JavaDoc packageInfoClass = null;
101         
102         try {
103             if (classLoader != null) {
104                 packageInfoClass =
105                     classLoader.loadClass(packageInfoClassName);
106             }
107             else {
108                 packageInfoClass = Class.forName(packageInfoClassName);
109             }
110         }
111         catch (Throwable JavaDoc t) {
112         }
113
114         if (packageInfoClass != null) {
115             try {
116                 initFromPackageInfo(pd, packageInfoClass);
117             }
118             catch (Throwable JavaDoc t) {
119             }
120         }
121         else {
122
123             Package JavaDoc pkg = null;
124             
125             if (classLoader != null) {
126                 // Get around getPackage's protected access
127
PackageLoader packageLoader =
128                     new PackageLoader(classLoader);
129                 
130                 pkg = packageLoader.getPackage(packageName);
131             }
132             
133             if (pkg == null) {
134                 pkg = Package.getPackage(packageName);
135             }
136
137             if (pkg != null) {
138                 initFromPackage(pd, pkg);
139             }
140         }
141
142         return pd;
143     }
144
145     // Initialize the PackageDescriptor from the PackageInfo class
146
private static void initFromPackageInfo(PackageDescriptor pd,
147                                             Class JavaDoc packageInfoClass)
148     throws Exception JavaDoc {
149
150
151         pd.setExists(true);
152
153         Class JavaDoc[] ca = new Class JavaDoc[0];
154         Object JavaDoc[] oa = new Object JavaDoc[0];
155
156         pd.setSpecificationTitle(
157             (String JavaDoc) (packageInfoClass.getMethod("getSpecificationTitle",
158                                                  ca)).invoke(null, oa));
159
160         pd.setSpecificationVersion(
161             (String JavaDoc) (packageInfoClass.getMethod("getSpecificationVersion",
162                                                  ca)).invoke(null, oa));
163
164         pd.setSpecificationVendor(
165             (String JavaDoc) (packageInfoClass.getMethod("getSpecificationVendor",
166                                                  ca)).invoke(null, oa));
167
168         pd.setImplementationTitle(
169             (String JavaDoc) (packageInfoClass.getMethod("getImplementationTitle",
170                                                  ca)).invoke(null, oa));
171
172         pd.setImplementationVersion(
173             (String JavaDoc) (packageInfoClass.getMethod("getImplementationVersion",
174                                                  ca)).invoke(null, oa));
175
176         pd.setImplementationVendor(
177             (String JavaDoc) (packageInfoClass.getMethod("getImplementationVendor",
178                                                  ca)).invoke(null, oa));
179
180         pd.setBaseDirectory(
181             (String JavaDoc) (packageInfoClass.getMethod("getBaseDirectory",
182                                                  ca)).invoke(null, oa));
183
184         pd.setRepository(
185             (String JavaDoc) (packageInfoClass.getMethod("getRepository",
186                                                  ca)).invoke(null, oa));
187
188         pd.setUsername(
189             (String JavaDoc) (packageInfoClass.getMethod("getUsername",
190                                                  ca)).invoke(null, oa));
191
192         pd.setBuildMachine(
193             (String JavaDoc) (packageInfoClass.getMethod("getBuildMachine",
194                                                  ca)).invoke(null, oa));
195
196         pd.setGroup(
197             (String JavaDoc) (packageInfoClass.getMethod("getGroup",
198                                                  ca)).invoke(null, oa));
199
200         pd.setProject(
201             (String JavaDoc) (packageInfoClass.getMethod("getProject",
202                                                  ca)).invoke(null, oa));
203
204         pd.setBuildLocation(
205             (String JavaDoc) (packageInfoClass.getMethod("getBuildLocation",
206                                                  ca)).invoke(null, oa));
207
208         pd.setProduct(
209             (String JavaDoc) (packageInfoClass.getMethod("getProduct",
210                                                  ca)).invoke(null, oa));
211
212         pd.setProductVersion(
213             (String JavaDoc) (packageInfoClass.getMethod("getProductVersion",
214                                                  ca)).invoke(null, oa));
215
216         pd.setBuildNumber(
217             (String JavaDoc) (packageInfoClass.getMethod("getBuildNumber",
218                                                  ca)).invoke(null, oa));
219
220         pd.setBuildDate(
221             (Date JavaDoc) (packageInfoClass.getMethod("getBuildDate",
222                                                ca)).invoke(null, oa));
223
224
225     }
226
227     // Initialize the PackageDescriptor from the Package
228
private static void initFromPackage(PackageDescriptor pd,
229                                         Package JavaDoc pkg) {
230
231         pd.setExists(true);
232
233         String JavaDoc specificationTitle = pkg.getSpecificationTitle();
234         String JavaDoc specificationVersion = pkg.getSpecificationVersion();
235         String JavaDoc specificationVendor = pkg.getSpecificationVendor();
236
237         String JavaDoc implementationTitle = pkg.getImplementationTitle();
238         String JavaDoc implementationVersion = pkg.getImplementationVersion();
239         String JavaDoc implementationVendor = pkg.getImplementationVendor();
240
241         if (implementationTitle == null) {
242             implementationTitle = specificationTitle;
243         }
244
245         if (implementationVersion == null) {
246             implementationVersion = specificationVersion;
247         }
248
249         if (implementationVendor == null) {
250             implementationVendor = specificationVendor;
251         }
252
253
254         pd.setSpecificationTitle(specificationTitle);
255         pd.setSpecificationVersion(specificationVersion);
256         pd.setSpecificationVendor(specificationVendor);
257
258         pd.setImplementationTitle(implementationTitle);
259         pd.setImplementationVersion(implementationVersion);
260         pd.setImplementationVendor(implementationVendor);
261
262         pd.setProduct(implementationTitle);
263         pd.setProductVersion(implementationVersion);
264     }
265
266
267     
268     protected String JavaDoc mPackageName;
269     protected boolean mExists;
270
271     protected String JavaDoc mSpecificationTitle;
272     protected String JavaDoc mSpecificationVersion;
273     protected String JavaDoc mSpecificationVendor;
274     protected String JavaDoc mImplementationTitle;
275     protected String JavaDoc mImplementationVersion;
276     protected String JavaDoc mImplementationVendor;
277     protected String JavaDoc mBaseDirectory;
278     protected String JavaDoc mRepository;
279     protected String JavaDoc mUsername;
280     protected String JavaDoc mBuildMachine;
281     protected String JavaDoc mGroup;
282     protected String JavaDoc mProject;
283     protected String JavaDoc mBuildLocation;
284     protected String JavaDoc mProduct;
285     protected String JavaDoc mProductVersion;
286     protected String JavaDoc mBuildNumber;
287     protected Date JavaDoc mBuildDate;
288
289     
290     /**
291      * Creates a new PackageDescriptor for the specified packageName
292      */

293     public PackageDescriptor(String JavaDoc packageName) {
294         setPackageName(packageName);
295     }
296
297     public void setPackageName(String JavaDoc packageName) {
298         mPackageName = packageName;
299     }
300
301     public String JavaDoc getPackageName() {
302         return mPackageName;
303     }
304
305     /**
306      * Returns true if this PackageDescriptor was successfully initialized
307      * from PackageInfo or Package data.
308      */

309     public boolean getExists() {
310         return mExists;
311     }
312
313     public void setSpecificationTitle(String JavaDoc s) {
314         mSpecificationTitle = s;
315     }
316
317     public String JavaDoc getSpecificationTitle() {
318         return mSpecificationTitle;
319     }
320
321     public void setSpecificationVersion(String JavaDoc s) {
322         mSpecificationVersion = s;
323     }
324
325     public String JavaDoc getSpecificationVersion() {
326         return mSpecificationVersion;
327     }
328
329     public void setSpecificationVendor(String JavaDoc s) {
330         mSpecificationVendor = s;
331     }
332
333     public String JavaDoc getSpecificationVendor() {
334         return mSpecificationVendor;
335     }
336
337     public void setImplementationTitle(String JavaDoc s) {
338         mImplementationTitle = s;
339     }
340
341     public String JavaDoc getImplementationTitle() {
342         return mImplementationTitle;
343     }
344
345     public void setImplementationVersion(String JavaDoc s) {
346         mImplementationVersion = s;
347     }
348
349     public String JavaDoc getImplementationVersion() {
350         return mImplementationVersion;
351     }
352
353     public void setImplementationVendor(String JavaDoc s) {
354         mImplementationVendor = s;
355     }
356
357     public String JavaDoc getImplementationVendor() {
358         return mImplementationVendor;
359     }
360
361     public void setBaseDirectory(String JavaDoc s) {
362         mBaseDirectory = s;
363     }
364
365     public String JavaDoc getBaseDirectory() {
366         return mBaseDirectory;
367     }
368
369     public void setRepository(String JavaDoc s) {
370         mRepository = s;
371     }
372
373     public String JavaDoc getRepository() {
374         return mRepository;
375     }
376
377     public void setUsername(String JavaDoc s) {
378         mUsername = s;
379     }
380
381     public String JavaDoc getUsername() {
382         return mUsername;
383     }
384
385     public void setBuildMachine(String JavaDoc s) {
386         mBuildMachine = s;
387     }
388
389     public String JavaDoc getBuildMachine() {
390         return mBuildMachine;
391     }
392
393     public void setGroup(String JavaDoc s) {
394         mGroup = s;
395     }
396
397     public String JavaDoc getGroup() {
398         return mGroup;
399     }
400
401     public void setProject(String JavaDoc s) {
402         mProject = s;
403     }
404
405     public String JavaDoc getProject() {
406         return mProject;
407     }
408
409     public void setBuildLocation(String JavaDoc s) {
410         mBuildLocation = s;
411     }
412
413     public String JavaDoc getBuildLocation() {
414         return mBuildLocation;
415     }
416
417     public void setProduct(String JavaDoc s) {
418         mProduct = s;
419     }
420
421     public String JavaDoc getProduct() {
422         return mProduct;
423     }
424
425     public void setProductVersion(String JavaDoc s) {
426         mProductVersion = s;
427     }
428
429     public String JavaDoc getProductVersion() {
430         return mProductVersion;
431     }
432
433     public void setBuildNumber(String JavaDoc s) {
434         mBuildNumber = s;
435     }
436
437     public String JavaDoc getBuildNumber() {
438         return mBuildNumber;
439     }
440
441     public void setBuildDate(Date JavaDoc d) {
442         mBuildDate = d;
443     }
444
445     public Date JavaDoc getBuildDate() {
446         return mBuildDate;
447     }
448
449     private void setExists(boolean exists) {
450         mExists = exists;
451     }
452
453     private static class PackageLoader extends ClassLoader JavaDoc {
454         
455         PackageLoader(ClassLoader JavaDoc parent) {
456             super(parent);
457         }
458
459         public Package JavaDoc getPackage(String JavaDoc name) {
460             return super.getPackage(name);
461         }
462     }
463
464 }
465
Popular Tags