KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSX > magic > MagicClassFactory


1 /** (c) Brendan Macmillan 2002, licensed under GNU's GPL 2
2         (If your project is not GPL, then a fee is due - see license on website)
3     Website: http://www.csse.monash.edu.au/~bren/JSX
4     Website: http://freshmeat.net/projects/jsx
5     List (can read without subscribing, need to join to post):
6       http://groups.yahoo.com/group/JSX-ideas/messages
7         Commercial licensing enquiries only:
8             bren@objectlight.com */

9
10
11 package JSX.magic;
12 import java.util.*;
13
14 /** Select "under the hood" connections to private implementation of Java,
15     * based on spec version and impl vendor properties, and without compile-time
16     * dependancies
17     * Note: other's vendors implementations may require different connections
18     * - see list at end. This approach can accommodate them.
19     *
20     * Issue: how to arrange the detection and selection...
21     * - If statements?
22     * - A hash?
23     * - subclassing? (ie: first: 1.2, 1.3 or 1.4; then specific vendors; then...)
24     * - a matrix?
25     * - decision graph?
26     * NB: Efficiency is irrelevant (not inner loop); clarity is important.
27     **/

28 public class MagicClassFactory {
29     public static MagicClassI newInstance() {
30         Properties p = System.getProperties();
31
32     // Idea: make up an inner class, with properties to match; and a result to
33
// give. Run through an array of these. By factoring out the superstructure,
34
// it should be easier to see that the correct things are linked up.
35
// In fact, it might be better to do it by order, because it is easy to
36
// pick out mistakes when arranged in columns...
37
// Comments are most welcome!!
38

39         String JavaDoc specVersion = p.getProperty("java.specification.version");
40     String JavaDoc implVersion = p.getProperty("java.version");
41     String JavaDoc implVendor = p.getProperty("java.vendor");
42
43         String JavaDoc magicName = null; // flag error if not set! "JSX.magic.MagicClass13";
44
if (implVendor.equals("Sun Microsystems Inc.") && ( specVersion.equals("1.2") || specVersion.equals("1.3") ) )
45             magicName = "JSX.magic.MagicClass13"; // explicit (even tho == default)
46

47         if (implVendor.equals("Sun Microsystems Inc.") && specVersion.equals("1.4"))
48             magicName = "JSX.magic.MagicClass14"; // specific
49

50         if (implVendor.equals("IBM Corporation") && specVersion.equals("1.3") && implVersion.equals("1.3.0 subset"))
51             magicName = "JSX.magic.MagicClass13_j9"; // "j9" or "J9"?
52

53         if (implVendor.equals("Blackdown Java-Linux Team") && specVersion.equals("1.3") && implVersion.equals("1.3.1"))
54             magicName = "JSX.magic.MagicClass13";
55
56         if (implVendor.equals("Blackdown Java-Linux Team") && specVersion.equals("1.4") && implVersion.equals("1.4.1"))
57             magicName = "JSX.magic.MagicClass14"; // thanks to Egon
58

59         if (implVendor.equals("Apple Computer, Inc.") && specVersion.equals("1.3") && implVersion.equals("1.3.1"))
60             magicName = "JSX.magic.MagicClass13";
61
62         if (magicName==null) {
63             System.err.println("---ATTENTION!--- JSX could not recognize your implementation of java, which is:");
64             System.err.println("\timplVendor='"+implVendor+"'");
65             System.err.println("\tspecVersion='"+specVersion+"'");
66             System.err.println("\timplVersion='"+implVersion+"'");
67
68             String JavaDoc tryingVersion; // to provoke compiler error if not set
69
if (specVersion.equals("1.2") || specVersion.equals("1.3")) {
70                 tryingVersion = "1.3";
71                 magicName = "JSX.magic.MagicClass13"; // for both 1.2 and 1.3
72
} else if (specVersion.equals("1.4")) {
73                 tryingVersion = "1.4";
74                 magicName = "JSX.magic.MagicClass14";
75             } else {
76                 tryingVersion = "1.4"; // default to 1.4
77
magicName = "JSX.magic.MagicClass14";
78             }
79             System.err.println("In the meantime, JSX will try the standard implementation for Java "+tryingVersion+" - which will probably work");
80             System.err.println("Please post the above information to: jsx-ideas@yahoogroups.com");
81             System.err.println(" - in particular, please state whether the standard implementation worked or not, for both writing and reading.");
82         }
83 /*
84         if (specVersion.equals("1.2") || specVersion.equals("1.3"))
85             magicName = "JSX.magic.MagicClass13";
86         else if (specVersion.equals("1.4"))
87             magicName = "JSX.magic.MagicClass14";
88         else //default
89             magicName = "JSX.magic.MagicClass13";
90 */

91
92 /*
93 OTIs j9 jvm (David Tottey)
94     java.specification.version=1.3
95     java.specification.vendor=Sun Microsystems Inc.
96     java.version=1.3.0 subset
97     java.vendor=IBM Corporation
98 */

99 // Nice if this message came up, when a failure encountered:
100
// If this fails, please report to report to bren@mail.csse.monash.edu.au,
101
// with specVersion and implVendor.
102
try {
103             return (MagicClassI) Class.forName(magicName).newInstance();
104         } catch (InstantiationException JavaDoc cantMake) {
105             throw new InternalError JavaDoc("Can't instantiate "+magicName);
106         } catch (ClassNotFoundException JavaDoc noClass) {
107             throw new InternalError JavaDoc("Can't find "+magicName);
108         } catch (IllegalAccessException JavaDoc noAccess) {
109             throw new InternalError JavaDoc("Not permitted to access "+magicName);
110         }
111     }
112 }
113
114
115
116
117 /*
118 With gcj 3.2 and JSX 1.0.3.1 (*)
119
120 8<---------------------------------------------------
121 $ ./a.out
122 java.specification.version=1.3
123 java.specification.vendor=Sun Microsystems Inc.
124
125 java.version=3.2
126 java.vendor=Free Software Foundation, Inc.
127 8<---------------------------------------------------
128
129 (*) I used a small patch to compile JSX/ObjIn.java
130
131 (And as I said previously for gcj 3.1.1,
132 > /home/DEVENV/JSX1.0.3.0/XSLTimport.java
133 > [SNIP] javax.xml from JDK1.4 not available in gcj 3.1.1
134 > 6 errors
135 )
136
137 With orp 1.0.9,
138 8<---------------------------------------------------
139 $ orp TestProperties
140 java.specification.version=null
141 java.specification.vendor=null
142
143 java.version=GNU Classpath 0.02
144 java.vendor=Intel MRL
145 8<---------------------------------------------------
146
147 With kissme 0.21,
148 8<---------------------------------------------------
149 $ kissme TestProperties
150 java.specification.version=null
151 java.specification.vendor=null
152
153 java.version=1.2
154 java.vendor=John Leuner
155 8<---------------------------------------------------
156
157 With kaffe 1.0.5,
158 8<---------------------------------------------------
159 $ /usr/bin/java TestProperties
160 java.specification.version=1.2
161 java.specification.vendor=Sun Microsystems Inc.
162
163 java.version=1.0.5
164 java.vendor=Transvirtual Technologies, Inc.
165 8<---------------------------------------------------
166
167 Not yet tested with SableVM.
168
169 --
170 Benoît Sibaud
171 http://oumph.free.fr
172 */

173
Popular Tags