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: public class MbeansDescriptorsSerSource extends ModelerSource {
031: private static Log log = LogFactory
032: .getLog(MbeansDescriptorsSerSource.class);
033: Registry registry;
034: String location;
035: String type;
036: Object source;
037: List mbeans = new ArrayList();
038:
039: public void setRegistry(Registry reg) {
040: this .registry = reg;
041: }
042:
043: public void setLocation(String loc) {
044: this .location = loc;
045: }
046:
047: /** Used if a single component is loaded
048: *
049: * @param type
050: */
051: public void setType(String type) {
052: this .type = type;
053: }
054:
055: public void setSource(Object source) {
056: this .source = source;
057: }
058:
059: public List loadDescriptors(Registry registry, String location,
060: String type, Object source) throws Exception {
061: setRegistry(registry);
062: setLocation(location);
063: setType(type);
064: setSource(source);
065: execute();
066: return mbeans;
067: }
068:
069: public void execute() throws Exception {
070: if (registry == null)
071: registry = Registry.getRegistry();
072: long t1 = System.currentTimeMillis();
073: try {
074: InputStream stream = null;
075: if (source instanceof URL) {
076: stream = ((URL) source).openStream();
077: }
078: if (source instanceof InputStream) {
079: stream = (InputStream) source;
080: }
081: if (stream == null) {
082: throw new Exception("Can't process " + source);
083: }
084: ObjectInputStream ois = new ObjectInputStream(stream);
085: Thread.currentThread().setContextClassLoader(
086: ManagedBean.class.getClassLoader());
087: Object obj = ois.readObject();
088: //log.info("Reading " + obj);
089: ManagedBean beans[] = (ManagedBean[]) obj;
090: // after all are read without error
091: for (int i = 0; i < beans.length; i++) {
092: mbeans.add(beans[i]);
093: }
094:
095: } catch (Exception ex) {
096: log.error("Error reading descriptors " + source + " "
097: + ex.toString(), ex);
098: throw ex;
099: }
100: long t2 = System.currentTimeMillis();
101: log.info("Reading descriptors ( ser ) " + (t2 - t1));
102: }
103: }
|