1 | /* java.beans.Beans
|
---|
2 | Copyright (C) 1998, 1999 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.beans;
|
---|
40 |
|
---|
41 | import java.io.*;
|
---|
42 | import java.applet.*;
|
---|
43 | import gnu.java.io.*;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <code>Beans</code> provides some helper methods that allow the basic operations of Bean-ness.
|
---|
47 | *
|
---|
48 | * @author John Keiser
|
---|
49 | * @since JDK1.1
|
---|
50 | * @version 1.1.0, 29 Jul 1998
|
---|
51 | *
|
---|
52 | */
|
---|
53 | public class Beans {
|
---|
54 | static boolean designTime = false;
|
---|
55 | static boolean guiAvailable = true;
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Once again, we have a java.beans class with only
|
---|
60 | * static methods that can be instantiated. When
|
---|
61 | * will the madness end? :)
|
---|
62 | */
|
---|
63 | public Beans() {
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Allows you to instantiate a Bean. This method takes
|
---|
68 | * a ClassLoader from which to read the Bean and the
|
---|
69 | * name of the Bean.<P>
|
---|
70 | *
|
---|
71 | * The Bean name should be a dotted name, like a class.
|
---|
72 | * It can represent several things. Beans will search
|
---|
73 | * for the Bean using the name like this:<P>
|
---|
74 | * <OL>
|
---|
75 | * <LI>Searches for a serialized instance of the Bean
|
---|
76 | * using getResource(), mangling the Bean name by
|
---|
77 | * replacing the dots with slashes and appending .ser
|
---|
78 | * (for example, gnu.beans.BlahDeBlah would cause
|
---|
79 | * Beans to search for gnu/beans/BlahDeBlah.ser using
|
---|
80 | * getResource()).</LI>
|
---|
81 | * <LI>Searches for the Bean class using the beanName,
|
---|
82 | * and then instantiates it with the no-arg constructor.
|
---|
83 | * At that point, if it is an Applet, it provides it
|
---|
84 | * with AppletContext and AppletStub, and then calls
|
---|
85 | * init().</LI>
|
---|
86 | * </OL>
|
---|
87 | * @param cl the ClassLoader to use, or <CODE>null</CODE>
|
---|
88 | * to use the default ClassLoader.
|
---|
89 | * @param beanName the name of the Bean.
|
---|
90 | * @return the Bean.
|
---|
91 | * @XXX
|
---|
92 | */
|
---|
93 | public static Object instantiate(ClassLoader cl, String beanName) throws IOException, ClassNotFoundException {
|
---|
94 | Object bean;
|
---|
95 |
|
---|
96 | InputStream serStream;
|
---|
97 | if(cl == null) {
|
---|
98 | serStream = ClassLoader.getSystemResourceAsStream(beanName.replace('.','/')+".ser");
|
---|
99 | } else {
|
---|
100 | serStream = cl.getResourceAsStream(beanName.replace('.','/')+".ser");
|
---|
101 | }
|
---|
102 | if(serStream != null) {
|
---|
103 | if(cl == null) {
|
---|
104 | ObjectInputStream ois = new ObjectInputStream(serStream);
|
---|
105 | bean = ois.readObject();
|
---|
106 | } else {
|
---|
107 | ClassLoaderObjectInputStream ois = new ClassLoaderObjectInputStream(serStream, cl);
|
---|
108 | bean = ois.readObject();
|
---|
109 | }
|
---|
110 | } else if(cl == null) {
|
---|
111 | Class beanClass = Class.forName(beanName);
|
---|
112 | try {
|
---|
113 | bean = beanClass.newInstance();
|
---|
114 | } catch(IllegalAccessException E) {
|
---|
115 | bean = null;
|
---|
116 | } catch(InstantiationException E) {
|
---|
117 | bean = null;
|
---|
118 | }
|
---|
119 | } else {
|
---|
120 | Class beanClass = cl.loadClass(beanName);
|
---|
121 | try {
|
---|
122 | bean = beanClass.newInstance();
|
---|
123 | } catch(IllegalAccessException E) {
|
---|
124 | bean = null;
|
---|
125 | } catch(InstantiationException E) {
|
---|
126 | bean = null;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | if(bean instanceof Applet) {
|
---|
131 | Applet a = (Applet)bean;
|
---|
132 | //a.setAppletContext(???);
|
---|
133 | //a.setStub(???);
|
---|
134 | if(serStream == null) {
|
---|
135 | a.init();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | return bean;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Get the Bean as a different class type.
|
---|
144 | * This should be used instead of casting to get a new
|
---|
145 | * type view of a Bean, because in the future there may
|
---|
146 | * be new types of Bean, even Beans spanning multiple
|
---|
147 | * Objects.
|
---|
148 | * @param bean the Bean to cast.
|
---|
149 | * @param newClass the Class to cast it to.
|
---|
150 | * @return the Bean as a new view, or if the operation
|
---|
151 | * could not be performed, the Bean itself.
|
---|
152 | */
|
---|
153 | public static Object getInstanceOf(Object bean, Class newClass) {
|
---|
154 | return bean;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Determine whether the Bean can be cast to a different
|
---|
159 | * class type.
|
---|
160 | * This should be used instead of instanceof to determine
|
---|
161 | * a Bean's castability, because in the future there may
|
---|
162 | * be new types of Bean, even Beans spanning multiple
|
---|
163 | * Objects.
|
---|
164 | * @param bean the Bean to cast.
|
---|
165 | * @param newClass the Class to cast it to.
|
---|
166 | * @return whether the Bean can be cast to the class type
|
---|
167 | * in question.
|
---|
168 | */
|
---|
169 | public static boolean isInstanceOf(Object bean, Class newBeanClass) {
|
---|
170 | return newBeanClass.isInstance(bean);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Find out whether the GUI is available to use.
|
---|
175 | * Defaults to true.
|
---|
176 | * @return whether the GUI is available to use.
|
---|
177 | */
|
---|
178 | public static boolean isGuiAvailable() {
|
---|
179 | return guiAvailable;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Find out whether it is design time. Design time means
|
---|
184 | * we are in a RAD tool.
|
---|
185 | * Defaults to false.
|
---|
186 | * @return whether it is design time.
|
---|
187 | */
|
---|
188 | public static boolean isDesignTime() {
|
---|
189 | return designTime;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Set whether the GUI is available to use.
|
---|
194 | * @param guiAvailable whether the GUI is available to use.
|
---|
195 | */
|
---|
196 | public static void setGuiAvailable(boolean guiAvailable) throws SecurityException {
|
---|
197 | Beans.guiAvailable = guiAvailable;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Set whether it is design time. Design time means we
|
---|
202 | * are in a RAD tool.
|
---|
203 | * @param designTime whether it is design time.
|
---|
204 | */
|
---|
205 | public static void setDesignTime(boolean designTime) throws SecurityException {
|
---|
206 | Beans.designTime = designTime;
|
---|
207 | }
|
---|
208 | }
|
---|