Source for file OsidLoader.php

Documentation is available at OsidLoader.php

  1. <?php
  2. /**
  3. * OsidLoader loads a specific implementation of an Open Service Interface
  4. * Definition (OSID) with its getManager method. The getManager method loads
  5. * an instance of the OSID's OsidManager, assigns the manager's OsidContext,
  6. * assigns any configuration information, and returns the instance of the OSID
  7. * implementation. This usage of the getManager method in the OsidLoader is
  8. * how applications should bind a particular implementation to an OSID. The
  9. * value of this approach is that an application can defer which specific OSID
  10. * implementation is used until runtime. The specific implementation package
  11. * name can then be part of the configuration information rather than being
  12. * hard coded. Changing implementations is simplified with this approach.
  13. *
  14. * <p>
  15. * As an example, in order to create a new Hierarchy, an application does not
  16. * use the new operator. It uses the OsidLoader getManager method to get an
  17. * instance of a class that implements HierarchyManager (a subclass of
  18. * OsidManager). The application uses the HierarchyManager instance to create
  19. * the Hierarchy. It is the createHierarchy() method in some package (e.g.
  20. * org.osid.hierarchy.impl.HierarchyManager) which uses the new operator on
  21. * org.osid.hierarchy.impl.Hierarchy, casts it as
  22. * org.osid.hierarchy.Hierarchy, and returns it to the application. This
  23. * indirection offers the significant value of being able to change
  24. * implementations in one spot with one modification, namely by using a
  25. * implementation package name argument for the OsidLoader getManager method.
  26. * </p>
  27. *
  28. * <p>
  29. * Sample:
  30. * <blockquote>
  31. * org.osid.OsidContext myContext = new org.osid.OsidContext();<br>
  32. * String key = "myKey";<br>
  33. * myContext.assignContext(key, "I want to save this string as context");<br>
  34. * String whatWasMyContext = myContext.getContext(key);<br>
  35. * org.osid.hierarchy.HierarchyManager hierarchyManager =
  36. * <blockquote>
  37. * org.osid.OsidLoader.getManager("org.osid.hierarchy.HierarchyManager","org.osid.shared.impl",myContext,null);
  38. * </blockquote>
  39. * org.osid.hierarchy.Hierarchy myHierarchy =
  40. * hierarchyManager.createHierarchy(...);<br>
  41. * </blockquote>
  42. * </p>
  43. *
  44. * <p>
  45. * A similar technique can be used for creating other objects. OSIDs that have
  46. * OsidManager implementations loaded by OsidLoader, will define an
  47. * appropriate interface to create these objects.
  48. * </p>
  49. *
  50. * <p>
  51. * The arguments to OsidLoader.getManager method are the OSID OsidManager
  52. * interface name, the implementing package name, the OsidContext, and any
  53. * additional configuration information.
  54. * </p>
  55. *
  56. * <p>
  57. * OSID Version: 2.0
  58. * </p>
  59. *
  60. * <p>
  61. * Licensed under the {@link org.osid.SidImplementationLicenseMIT MIT}
  62. * O.K.I&#46; OSID Definition License}.
  63. * </p>
  64. *
  65. * @package org.osid
  66. */
  67. class OsidLoader
  68. extends stdClass
  69. {
  70. /**
  71. * Returns an instance of the OsidManager of the OSID specified by the OSID
  72. * package OsidManager interface name and the implementation package name.
  73. * The implementation class name is constructed from the SID package
  74. * Manager interface name. A configuration file name is constructed in a
  75. * similar manner and if the file exists it is loaded into the
  76. * implementation's OsidManager's configuration.
  77. *
  78. * <p>
  79. * Example: To load an implementation of the org.osid.Filing OSID
  80. * implemented in a package "xyz", one would use:
  81. * </p>
  82. *
  83. * <p>
  84. * org.osid.filing.FilingManager fm =
  85. * (org.osid.filing.FilingManager)org.osid.OsidLoader.getManager(
  86. * </p>
  87. *
  88. * <p>
  89. * "org.osid.filing.FilingManager" ,
  90. * </p>
  91. *
  92. * <p>
  93. * "xyz" ,
  94. * </p>
  95. *
  96. * <p>
  97. * new org.osid.OsidContext());
  98. * </p>
  99. *
  100. * @param string $osidPackageManagerName
  101. * @param string $implPackageName
  102. * @param object OsidContext $context
  103. * @param object Properties $additionalConfiguration (original type: java.util.Properties)
  104. *
  105. * @return object OsidManager
  106. *
  107. * @throws object OsidException An exception with one of the following
  108. * messages defined in org.osid.OsidException: {@link }
  109. * org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
  110. * {@link org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT},
  111. * {@link org.osid.OsidException#VERSION_ERROR VERSION_ERROR},
  112. * ={@link org.osid.OsidException#INTERFACE_NOT_FOUND}
  113. * INTERFACE_NOT_FOUND}, ={@link }
  114. * org.osid.OsidException#MANAGER_NOT_FOUND MANAGER_NOT_FOUND},
  115. * ={@link org.osid.OsidException#MANAGER_INSTANTIATION_ERROR}
  116. * MANAGER_INSTANTIATION_ERROR}, ={@link }
  117. * org.osid.OsidException#ERROR_ASSIGNING_CONTEXT
  118. * ERROR_ASSIGNING_CONTEXT}, ={@link }
  119. * org.osid.OsidException#ERROR_ASSIGNING_CONFIGURATION
  120. * ERROR_ASSIGNING_CONFIGURATION}
  121. *
  122. * @access public
  123. * @static
  124. */
  125. function getManager ( $osidPackageManagerName, $implPackageName, $context, $additionalConfiguration )
  126. {
  127. /* try {
  128. if ((null != context) && (null != osidPackageManagerName) &&
  129. (null != implPackageName)) {
  130. String osidInterfaceName = osidPackageManagerName;
  131. String className = makeClassName(osidPackageManagerName);
  132. String managerClassName = makeFullyQualifiedClassName(implPackageName,
  133. className);
  134.  
  135. Class osidInterface = Class.forName(osidInterfaceName);
  136.  
  137. if (null != osidInterface) {
  138. Class managerClass = Class.forName(managerClassName);
  139.  
  140. if (null != managerClass) {
  141. if (osidInterface.isAssignableFrom(managerClass)) {
  142. OsidManager manager = (OsidManager) managerClass.newInstance();
  143.  
  144. if (null != manager) {
  145. try {
  146. manager.osidVersion_2_0();
  147. } catch (Throwable ex) {
  148. throw new OsidException(OsidException.VERSION_ERROR);
  149. }
  150.  
  151. try {
  152. manager.assignOsidContext(context);
  153. } catch (Exception ex) {
  154. throw new OsidException(OsidException.ERROR_ASSIGNING_CONTEXT);
  155. }
  156.  
  157. try {
  158. java.util.Properties configuration = getConfiguration(manager);
  159.  
  160. if (null == configuration) {
  161. configuration = new java.util.Properties();
  162. }
  163.  
  164. if (null != additionalConfiguration) {
  165. java.util.Enumeration enum = additionalConfiguration.propertyNames();
  166.  
  167. while (enum.hasMoreElements()) {
  168. java.io.Serializable key = (java.io.Serializable) enum.nextElement();
  169.  
  170. if (null != key) {
  171. java.io.Serializable value = (java.io.Serializable) additionalConfiguration.get(key);
  172.  
  173. if (null != value) {
  174. configuration.put(key, value);
  175. }
  176. }
  177. }
  178. }
  179.  
  180. manager.assignConfiguration(configuration);
  181.  
  182. return manager;
  183. } catch (Exception ex) {
  184. throw new OsidException(OsidException.ERROR_ASSIGNING_CONFIGURATION);
  185. }
  186. }
  187.  
  188. throw new OsidException(OsidException.MANAGER_INSTANTIATION_ERROR);
  189. }
  190.  
  191. throw new OsidException(OsidException.MANAGER_NOT_OSID_IMPLEMENTATION);
  192. }
  193.  
  194. throw new OsidException(OsidException.MANAGER_NOT_FOUND);
  195. }
  196.  
  197. throw new OsidException(OsidException.INTERFACE_NOT_FOUND);
  198. }
  199.  
  200. throw new OsidException(OsidException.NULL_ARGUMENT);
  201. } catch (OsidException oex) {
  202. throw new OsidException(oex.getMessage());
  203. } catch (java.lang.Throwable ex) {
  204. throw new org.osid.OsidException(org.osid.OsidException.OPERATION_FAILED);
  205. }
  206. */ }
  207.  
  208. /*private static java.lang.String makeClassName(java.lang.String packageManagerName) throws org.osid.OsidException{
  209. String className = packageManagerName;
  210.  
  211. if (null != className) {
  212. className = (className.endsWith(".")
  213. ? className.substring(0, className.length() - 1) : className);
  214.  
  215. int lastdot = className.lastIndexOf(".");
  216.  
  217. if (-1 != lastdot) {
  218. className = className.substring(lastdot + 1);
  219. }
  220. }
  221.  
  222. return className;
  223. }
  224.  
  225. private static java.lang.String makeFullyQualifiedClassName(java.lang.String packageName, java.lang.String className) throws org.osid.OsidException{
  226. String cName = className;
  227.  
  228. if (null != packageName) {
  229. String pName = (packageName.endsWith(".") ? packageName
  230. : new String(packageName +
  231. "."));
  232. cName = pName + className;
  233. }
  234.  
  235. return cName;
  236. }
  237.  
  238. private static java.util.Properties getConfiguration(org.osid.OsidManager manager) throws org.osid.OsidException{
  239. java.util.Properties properties = null;
  240.  
  241. if (null != manager) {
  242. Class managerClass = manager.getClass();
  243.  
  244. try {
  245. String managerClassName = managerClass.getName();
  246. int index = managerClassName.lastIndexOf(".");
  247.  
  248. if (-1 != index) {
  249. managerClassName = managerClassName.substring(index + 1);
  250. }
  251.  
  252. java.io.InputStream is = managerClass.getResourceAsStream(managerClassName +
  253. ".properties");
  254.  
  255. if (null != is) {
  256. properties = new java.util.Properties();
  257. properties.load(is);
  258. }
  259. } catch (Throwable ex) {
  260. }
  261. }
  262.  
  263. return properties;
  264. }
  265. */
  266. }
  267.  
  268. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:24 -0400 by phpDocumentor 1.3.0RC3