View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.modeler.modules;
18  
19  import java.io.InputStream;
20  import java.io.ObjectInputStream;
21  import java.net.URL;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.modeler.ManagedBean;
28  import org.apache.commons.modeler.Registry;
29  
30  
31  public class MbeansDescriptorsSerSource extends ModelerSource
32  {
33      private static Log log = LogFactory.getLog(MbeansDescriptorsSerSource.class);
34      Registry registry;
35      String location;
36      String type;
37      Object source;
38      List mbeans=new ArrayList();
39  
40      public void setRegistry(Registry reg) {
41          this.registry=reg;
42      }
43  
44      public void setLocation( String loc ) {
45          this.location=loc;
46      }
47  
48      /*** Used if a single component is loaded
49       *
50       * @param type
51       */
52      public void setType( String type ) {
53         this.type=type;
54      }
55  
56      public void setSource( Object source ) {
57          this.source=source;
58      }
59  
60      public List loadDescriptors( Registry registry, String location,
61                                   String type, Object source)
62              throws Exception
63      {
64          setRegistry(registry);
65          setLocation(location);
66          setType(type);
67          setSource(source);
68          execute();
69          return mbeans;
70      }
71  
72      public void execute() throws Exception {
73          if( registry==null ) registry=Registry.getRegistry();
74          long t1=System.currentTimeMillis();
75          try {
76              InputStream stream=null;
77              if( source instanceof URL ) {
78                  stream=((URL)source).openStream();
79              }
80              if( source instanceof InputStream ) {
81                  stream=(InputStream)source;
82              }
83              if( stream==null ) {
84                  throw new Exception( "Can't process "+ source);
85              }
86              ObjectInputStream ois=new ObjectInputStream(stream);
87              Thread.currentThread().setContextClassLoader(ManagedBean.class.getClassLoader());
88              Object obj=ois.readObject();
89              //log.info("Reading " + obj);
90              ManagedBean beans[]=(ManagedBean[])obj;
91              // after all are read without error
92              for( int i=0; i<beans.length; i++ ) {
93                  mbeans.add(beans[i]);
94              }
95  
96          } catch( Exception ex ) {
97              log.error( "Error reading descriptors " + source + " " +  ex.toString(),
98                      ex);
99              throw ex;
100         }
101         long t2=System.currentTimeMillis();
102         log.info( "Reading descriptors ( ser ) " + (t2-t1));
103     }
104 }