001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.modeler.modules;
018
019 import java.io.InputStream;
020 import java.io.ObjectInputStream;
021 import java.net.URL;
022 import java.util.ArrayList;
023 import java.util.List;
024
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.apache.commons.modeler.ManagedBean;
028 import org.apache.commons.modeler.Registry;
029
030
031 public class MbeansDescriptorsSerSource extends ModelerSource
032 {
033 private static Log log = LogFactory.getLog(MbeansDescriptorsSerSource.class);
034 Registry registry;
035 String location;
036 String type;
037 Object source;
038 List mbeans=new ArrayList();
039
040 public void setRegistry(Registry reg) {
041 this.registry=reg;
042 }
043
044 public void setLocation( String loc ) {
045 this.location=loc;
046 }
047
048 /** Used if a single component is loaded
049 *
050 * @param type
051 */
052 public void setType( String type ) {
053 this.type=type;
054 }
055
056 public void setSource( Object source ) {
057 this.source=source;
058 }
059
060 public List loadDescriptors( Registry registry, String location,
061 String type, Object source)
062 throws Exception
063 {
064 setRegistry(registry);
065 setLocation(location);
066 setType(type);
067 setSource(source);
068 execute();
069 return mbeans;
070 }
071
072 public void execute() throws Exception {
073 if( registry==null ) registry=Registry.getRegistry();
074 long t1=System.currentTimeMillis();
075 try {
076 InputStream stream=null;
077 if( source instanceof URL ) {
078 stream=((URL)source).openStream();
079 }
080 if( source instanceof InputStream ) {
081 stream=(InputStream)source;
082 }
083 if( stream==null ) {
084 throw new Exception( "Can't process "+ source);
085 }
086 ObjectInputStream ois=new ObjectInputStream(stream);
087 Thread.currentThread().setContextClassLoader(ManagedBean.class.getClassLoader());
088 Object obj=ois.readObject();
089 //log.info("Reading " + obj);
090 ManagedBean beans[]=(ManagedBean[])obj;
091 // after all are read without error
092 for( int i=0; i<beans.length; i++ ) {
093 mbeans.add(beans[i]);
094 }
095
096 } catch( Exception ex ) {
097 log.error( "Error reading descriptors " + source + " " + ex.toString(),
098 ex);
099 throw ex;
100 }
101 long t2=System.currentTimeMillis();
102 log.info( "Reading descriptors ( ser ) " + (t2-t1));
103 }
104 }