1. package org.apache.commons.modeler.modules;
  2. import java.io.InputStream;
  3. import java.io.ObjectInputStream;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.apache.commons.modeler.ManagedBean;
  10. import org.apache.commons.modeler.Registry;
  11. public class MbeansDescriptorsSerSource extends ModelerSource
  12. {
  13. private static Log log = LogFactory.getLog(MbeansDescriptorsSerSource.class);
  14. Registry registry;
  15. String location;
  16. String type;
  17. Object source;
  18. List mbeans=new ArrayList();
  19. public void setRegistry(Registry reg) {
  20. this.registry=reg;
  21. }
  22. public void setLocation( String loc ) {
  23. this.location=loc;
  24. }
  25. /** Used if a single component is loaded
  26. *
  27. * @param type
  28. */
  29. public void setType( String type ) {
  30. this.type=type;
  31. }
  32. public void setSource( Object source ) {
  33. this.source=source;
  34. }
  35. public List loadDescriptors( Registry registry, String location,
  36. String type, Object source)
  37. throws Exception
  38. {
  39. setRegistry(registry);
  40. setLocation(location);
  41. setType(type);
  42. setSource(source);
  43. execute();
  44. return mbeans;
  45. }
  46. public void execute() throws Exception {
  47. if( registry==null ) registry=Registry.getRegistry();
  48. long t1=System.currentTimeMillis();
  49. try {
  50. InputStream stream=null;
  51. if( source instanceof URL ) {
  52. stream=((URL)source).openStream();
  53. }
  54. if( source instanceof InputStream ) {
  55. stream=(InputStream)source;
  56. }
  57. if( stream==null ) {
  58. throw new Exception( "Can't process "+ source);
  59. }
  60. ObjectInputStream ois=new ObjectInputStream(stream);
  61. Thread.currentThread().setContextClassLoader(ManagedBean.class.getClassLoader());
  62. Object obj=ois.readObject();
  63. //log.info("Reading " + obj);
  64. ManagedBean beans[]=(ManagedBean[])obj;
  65. // after all are read without error
  66. for( int i=0; i<beans.length; i++ ) {
  67. mbeans.add(beans[i]);
  68. }
  69. } catch( Exception ex ) {
  70. log.error( "Error reading descriptors " + source + " " + ex.toString(),
  71. ex);
  72. throw ex;
  73. }
  74. long t2=System.currentTimeMillis();
  75. log.info( "Reading descriptors ( ser ) " + (t2-t1));
  76. }
  77. }