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 gnu.java.io.ClassLoaderObjectInputStream;
42 import java.io.InputStream;
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.applet.Applet;
46 
47 /**
48  * <code>Beans</code> provides some helper methods that allow the basic
49  * operations of Bean-ness.
50  *
51  * @author John Keiser
52  * @since 1.1
53  * @version 1.1.0, 29 Jul 1998
54  *
55  */
56 public class Beans
57 {
58   static boolean designTime = false;
59   static boolean guiAvailable = true;
60 
61   /**
62    * Once again, we have a java.beans class with only
63    * static methods that can be instantiated.  When
64    * will the madness end? :)
65    */
Beans()66   public Beans()
67   {
68     // Do nothing here.
69   }
70 
71   /**
72    * Allows you to instantiate a Bean.  This method takes
73    * a ClassLoader from which to read the Bean and the
74    * name of the Bean.<P>
75    *
76    * The Bean name should be a dotted name, like a class.
77    * It can represent several things.  Beans will search
78    * for the Bean using the name like this:<P>
79    * <OL>
80    * <LI>Searches for a serialized instance of the Bean
81    * using getResource(), mangling the Bean name by
82    * replacing the dots with slashes and appending .ser
83    * (for example, gnu.beans.BlahDeBlah would cause
84    * Beans to search for gnu/beans/BlahDeBlah.ser using
85    * getResource()).</LI>
86    * <LI>Searches for the Bean class using the beanName,
87    * and then instantiates it with the no-arg constructor.
88    * At that point, if it is an Applet, it provides it
89    * with AppletContext and AppletStub, and then calls
90    * init().</LI>
91    * </OL>
92    *
93    * @param cl the ClassLoader to use, or <CODE>null</CODE>
94    *        to use the default ClassLoader.
95    * @param beanName the name of the Bean.
96    *
97    * @return the Bean.
98    *
99    * @XXX
100    */
instantiate(ClassLoader cl, String beanName)101   public static Object instantiate (ClassLoader cl, String beanName)
102     throws IOException, ClassNotFoundException
103   {
104     Object bean;
105     InputStream serStream;
106 
107     if (cl == null)
108       {
109         serStream = ClassLoader.getSystemResourceAsStream
110           (beanName.replace ('.','/')+".ser");
111       }
112     else
113       {
114         serStream = cl.getResourceAsStream (beanName.replace ('.', '/')
115                                             + ".ser");
116       }
117 
118     if (serStream != null)
119       {
120         if(cl == null)
121           {
122             ObjectInputStream ois = new ObjectInputStream(serStream);
123             bean = ois.readObject();
124           }
125         else
126           {
127             ClassLoaderObjectInputStream ois =
128               new ClassLoaderObjectInputStream (serStream, cl);
129             bean = ois.readObject();
130           }
131       }
132     else if(cl == null)
133       {
134         Class beanClass = Class.forName(beanName);
135         try
136           {
137             bean = beanClass.newInstance();
138           }
139         catch(IllegalAccessException E)
140           {
141             bean = null;
142           }
143         catch(InstantiationException E)
144           {
145             bean = null;
146           }
147       }
148     else
149       {
150         Class beanClass = cl.loadClass(beanName);
151         try
152           {
153             bean = beanClass.newInstance();
154           }
155         catch(IllegalAccessException E)
156           {
157             bean = null;
158           }
159         catch(InstantiationException E)
160           {
161             bean = null;
162           }
163       }
164 
165     if(bean instanceof Applet)
166       {
167         Applet a = (Applet)bean;
168         //a.setAppletContext(???);
169         //a.setStub(???);
170         if(serStream == null)
171           {
172             a.init();
173           }
174       }
175 
176     return bean;
177   }
178 
179   /**
180    * Get the Bean as a different class type.
181    * This should be used instead of casting to get a new
182    * type view of a Bean, because in the future there may
183    * be new types of Bean, even Beans spanning multiple
184    * Objects.
185    *
186    * @param bean the Bean to cast.
187    * @param newClass the Class to cast it to.
188    *
189    * @return the Bean as a new view, or if the operation
190    *         could not be performed, the Bean itself.
191    */
getInstanceOf(Object bean, Class newClass)192   public static Object getInstanceOf(Object bean, Class newClass)
193   {
194     return bean;
195   }
196 
197   /**
198    * Determine whether the Bean can be cast to a different
199    * class type.
200    * This should be used instead of instanceof to determine
201    * a Bean's castability, because in the future there may
202    * be new types of Bean, even Beans spanning multiple
203    * Objects.
204    *
205    * @param bean the Bean to cast.
206    * @param newClass the Class to cast it to.
207    *
208    * @return whether the Bean can be cast to the class type
209    *         in question.
210    */
isInstanceOf(Object bean, Class newBeanClass)211   public static boolean isInstanceOf(Object bean, Class newBeanClass)
212   {
213     return newBeanClass.isInstance(bean);
214   }
215 
216   /**
217    * Find out whether the GUI is available to use.
218    * Defaults to true.
219    *
220    * @return whether the GUI is available to use.
221    */
isGuiAvailable()222   public static boolean isGuiAvailable()
223   {
224     return guiAvailable;
225   }
226 
227   /**
228    * Find out whether it is design time.  Design time means
229    * we are in a RAD tool.
230    * Defaults to false.
231    *
232    * @return whether it is design time.
233    */
isDesignTime()234   public static boolean isDesignTime()
235   {
236     return designTime;
237   }
238 
239   /**
240    * Set whether the GUI is available to use.
241    * @param guiAvailable whether the GUI is available to use.
242    */
setGuiAvailable(boolean guiAvailable)243   public static void setGuiAvailable(boolean guiAvailable)
244     throws SecurityException
245   {
246     Beans.guiAvailable = guiAvailable;
247   }
248 
249   /**
250    * Set whether it is design time.  Design time means we
251    * are in a RAD tool.
252    *
253    * @param designTime whether it is design time.
254    */
setDesignTime(boolean designTime)255   public static void setDesignTime(boolean designTime)
256     throws SecurityException
257   {
258     Beans.designTime = designTime;
259   }
260 }
261