Beans.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* java.beans.Beans
  2. Copyright (C) 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. import gnu.java.beans.DummyAppletStub;
  33. import gnu.java.io.ClassLoaderObjectInputStream;
  34. import java.applet.Applet;
  35. import java.beans.beancontext.BeanContext;
  36. import java.io.IOException;
  37. import java.io.ObjectInputStream;
  38. import java.net.URL;
  39. /**
  40. * <code>Beans</code> provides some helper methods that allow the basic
  41. * operations of Bean-ness.
  42. *
  43. * @author John Keiser
  44. * @author Robert Schuster
  45. *
  46. * @since 1.1
  47. * @status updated to 1.4
  48. *
  49. */
  50. public class Beans
  51. {
  52. static boolean designTime = false;
  53. static boolean guiAvailable = true;
  54. /**
  55. * Once again, we have a java.beans class with only
  56. * static methods that can be instantiated. When
  57. * will the madness end? :)
  58. */
  59. public Beans()
  60. {
  61. // Does intentionally nothing here.
  62. }
  63. /** Creates a bean.
  64. * <p>This is a convenience method that calls <code>instantiate(cl, beanName, null, null)</code>.</p>
  65. *
  66. * @see instantiate(ClassLoader, String, BeanContext, AppletInitializer)
  67. * @param cl ClassLoader to be used or <code>null</code> for the system classloader.
  68. * @param beanName Name of a serialized bean or class name.
  69. * @return A newly created bean.
  70. * @throws IOException If access of an IO resource failed.
  71. * @throws ClassNotFoundException If the class name is not known or does not lead to a proper bean class.
  72. */
  73. public static Object instantiate(ClassLoader cl, String beanName)
  74. throws IOException, ClassNotFoundException
  75. {
  76. return instantiate(cl, beanName, null, null);
  77. }
  78. /** Creates a bean.
  79. *
  80. * <p>This is a convenience method that calls <code>instantiate(cl, beanName, beanContext, null)</code>.</p>
  81. *
  82. * @see instantiate(ClassLoader, String, BeanContext, AppletInitializer)
  83. * @param cl ClassLoader to be used or <code>null</code> for the system classloader.
  84. * @param beanName Name of a serialized bean or class name.
  85. * @param beanContext Context to which the newly created Bean should be added.
  86. * @return A newly created bean.
  87. * @throws IOException If access of an IO resource failed.
  88. * @throws ClassNotFoundException If the class name is not known or does not lead to a proper bean class.
  89. */
  90. public static Object instantiate(
  91. ClassLoader cl,
  92. String beanName,
  93. BeanContext beanContext)
  94. throws IOException, ClassNotFoundException
  95. {
  96. return instantiate(cl, beanName, beanContext, null);
  97. }
  98. /** Instantiates a bean according to Beans 1.0.
  99. *
  100. * <p>In Beans 1.0 the instantiation scheme is as follows:</p>
  101. * <p>The name should be dot-separated (e.g "place.for.beans.myBean") and indicate either a
  102. * serialized object or a class name. In the first case all dots in the name are replaced with
  103. * slashes ('/') and ".ser" is appended ("place.for.beans.myBean" becomes "place/for/beans/myBean.ser").
  104. * The bean is then loaded as an application or system resource depending on whether a
  105. * <code>ClassLoader</code> was provided.</p>
  106. *
  107. * <p>If no such resource exists or if it contains no bean the name is interpreted as a class name of
  108. * which an instance is then created.</p>
  109. *
  110. * <p>If a <code>BeanContext</code> instance is available the created bean is added to it.</p>
  111. *
  112. * <p>If the created Bean is an <code>Applet</code> or subclass and an <code>AppletInitializer</code>
  113. * instance is available the applet is initialized and afterwards activated using the initializer. Additionally
  114. * every instantiated <code>Applet</code> bean is initialized using the {@link Applet.init} method.
  115. * Furthermore every applet gets a default <code>AppletStub</code>. The <code>Applet</code>'s
  116. * document base is the location of the ".ser" file if it was deserialized or the location of its class
  117. * file if it was instantiated.</p>
  118. *
  119. * <p>A <code>ClassNotFoundException</code> is not only thrown when a class name was unknown
  120. * but even when the class has public no-argument constructor
  121. * (<code>IllegalAccessException</code> is wrapped) or an exception is thrown while
  122. * invoking such a constructor (causing exception is wrapped).</p>
  123. *
  124. * @param cl ClassLoader to be used or <code>null</code> for the system classloader.
  125. * @param beanName Name of a serialized bean or class name.
  126. * @param beanContext Context to which the newly created Bean should be added.
  127. * @param initializer The AppletInitializer which is used for initializing <code>Applet</code> beans.
  128. * @return A newly created bean.
  129. * @throws IOException If access of an IO resource failed.
  130. * @throws ClassNotFoundException If the class name is not known or does not lead to a proper bean class.
  131. */
  132. public static Object instantiate(
  133. ClassLoader cl,
  134. String beanName,
  135. BeanContext beanContext,
  136. AppletInitializer initializer)
  137. throws IOException, ClassNotFoundException
  138. {
  139. Object bean = null;
  140. URL beanLocation = null;
  141. URL classLocation = null;
  142. // Converts bean name into a resource name (eg. "a.b.c" -> "a/b/c").
  143. String resourceName = beanName.replace('.', '/');
  144. /* Tries to get an input stream of the Bean, reading it as a system resource
  145. * if no ClassLoader is present or as an application resource if a classloader
  146. * is given.
  147. */
  148. beanLocation =
  149. (cl == null)
  150. ? ClassLoader.getSystemResource(resourceName + ".ser")
  151. : cl.getResource(resourceName + ".ser");
  152. // Reads the serialized Bean from the returned URL.
  153. if (beanLocation != null)
  154. {
  155. // Deserializes the bean instance.
  156. ObjectInputStream ois =
  157. (cl == null)
  158. ? new ObjectInputStream(beanLocation.openStream())
  159. : new ClassLoaderObjectInputStream(
  160. beanLocation.openStream(),
  161. cl);
  162. bean = ois.readObject();
  163. /* Implementation note: The result of ObjectInputStream.readObject()
  164. * may have been null at this point (its a valid value to deserialize)
  165. * and we explicitly want to try instantiation in such a case
  166. * (this is important for compatibility).
  167. */
  168. }
  169. // Instantiates the Bean using reflective instantiation if it has not been created yet.
  170. if (bean == null)
  171. {
  172. // Makes sure that the deserialization was NOT done.
  173. beanLocation = null;
  174. Class beanClass;
  175. if (cl == null)
  176. {
  177. beanClass = Class.forName(beanName);
  178. classLocation =
  179. ClassLoader.getSystemResource(resourceName + ".class");
  180. }
  181. else
  182. {
  183. beanClass = cl.loadClass(beanName);
  184. classLocation = cl.getResource(resourceName + ".class");
  185. }
  186. // Instantiates and optionally registers the new bean.
  187. try
  188. {
  189. bean = beanClass.newInstance();
  190. }
  191. catch(Exception e) {
  192. /* Wraps all kinds of Exceptions in a ClassNotFoundException (this behavior
  193. * matches with official >= 1.5, this was different for <=1.4)
  194. */
  195. throw new ClassNotFoundException(null, e);
  196. }
  197. }
  198. /* Applet beans are treated in the following way:
  199. * - all AppletS get a default AppletStub
  200. * - all AppletS are initialized using the AppletInitializer instance (if it is available)
  201. * - as every other Bean Applets are added to a BeanContext if one is available
  202. * - each instantiated Applet is initialized using Applet.init() (this is not done for deserialized ones)
  203. * - finally AppletS get activated using the AppletInitializerS activate-Method
  204. *
  205. * The order of operations is important for compatibility.
  206. */
  207. Applet applet = null;
  208. if (bean instanceof Applet)
  209. {
  210. // Makes a second instanceof call unneccessary (instanceof is expensive).
  211. applet = (Applet) bean;
  212. /* The AppletStub's code and document base is set as follows:
  213. * The code base is always the URL from where the class data originated
  214. * (without the package name).
  215. * If the Applet was deserialized the document base is the location of
  216. * the serialized instance (usually the ".ser" file) otherwise its the URL
  217. * from where the class data originated (usually the absolute directory
  218. * location of the ".class" file).
  219. */
  220. applet.setStub(
  221. new DummyAppletStub(
  222. applet
  223. .getClass()
  224. .getProtectionDomain()
  225. .getCodeSource()
  226. .getLocation(),
  227. (beanLocation == null) ? classLocation : beanLocation));
  228. // Runs the Applet's initialization using an AppletInitializer.
  229. if (initializer != null)
  230. {
  231. initializer.initialize(applet, beanContext);
  232. }
  233. }
  234. // Adds the new bean to its BeanContext.
  235. if (beanContext != null)
  236. {
  237. beanContext.add(bean);
  238. }
  239. if (applet != null)
  240. {
  241. // Initializes an instantiated (not deserialized) Applet using its own method.
  242. if (beanLocation == null)
  243. {
  244. applet.init();
  245. }
  246. // Runs the Applet's activation using an AppletInitializer.
  247. if (initializer != null)
  248. {
  249. initializer.activate(applet);
  250. }
  251. }
  252. return bean;
  253. }
  254. /**
  255. * Returns the Bean as a different class type.
  256. * This should be used instead of casting to get a new
  257. * type view of a Bean, because in the future there may
  258. * be new types of Bean, even Beans spanning multiple
  259. * Objects.
  260. *
  261. * @param bean the Bean to cast.
  262. * @param newClass the Class to cast it to.
  263. *
  264. * @return the Bean as a new view, or if the operation
  265. * could not be performed, the Bean itself.
  266. */
  267. public static Object getInstanceOf(Object bean, Class<?> newClass)
  268. {
  269. return bean;
  270. }
  271. /**
  272. * Determines whether the Bean can be cast to a different
  273. * class type.
  274. * This should be used instead of instanceof to determine
  275. * a Bean's castability, because in the future there may
  276. * be new types of Bean, even Beans spanning multiple
  277. * Objects.
  278. *
  279. * @param bean the Bean to cast.
  280. * @param newBeanClass the Class to cast it to.
  281. *
  282. * @return whether the Bean can be cast to the class type
  283. * in question.
  284. */
  285. public static boolean isInstanceOf(Object bean, Class<?> newBeanClass)
  286. {
  287. return newBeanClass.isInstance(bean);
  288. }
  289. /**
  290. * Returns whether the GUI is available to use.
  291. * <p>Defaults to true.</p>
  292. *
  293. * @return whether the GUI is available to use.
  294. */
  295. public static boolean isGuiAvailable()
  296. {
  297. return guiAvailable;
  298. }
  299. /**
  300. * Returns whether it is design time. Design time means
  301. * we are in a RAD tool.
  302. * <p>Defaults to false.</p>
  303. *
  304. * @return whether it is design time.
  305. */
  306. public static boolean isDesignTime()
  307. {
  308. return designTime;
  309. }
  310. /**
  311. * Sets whether the GUI is available to use.
  312. *
  313. * @param guiAvailable whether the GUI is available to use.
  314. */
  315. public static void setGuiAvailable(boolean guiAvailable)
  316. throws SecurityException
  317. {
  318. Beans.guiAvailable = guiAvailable;
  319. }
  320. /**
  321. * Sets whether it is design time. Design time means we
  322. * are in a RAD tool.
  323. *
  324. * @param designTime whether it is design time.
  325. */
  326. public static void setDesignTime(boolean designTime)
  327. throws SecurityException
  328. {
  329. Beans.designTime = designTime;
  330. }
  331. }