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.File;
020 import java.io.FileInputStream;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.net.URL;
024 import java.util.List;
025
026 import javax.management.ObjectName;
027
028 import org.apache.commons.modeler.Registry;
029
030 /** Source for descriptor data. More sources can be added.
031 *
032 */
033 public class ModelerSource {
034 protected Object source;
035 protected String location;
036
037 /** Load data, returns a list of items.
038 *
039 * @param registry
040 * @param location
041 * @param type
042 * @param source Introspected object or some other source
043 * @throws Exception
044 */
045 public List loadDescriptors( Registry registry, String location,
046 String type, Object source)
047 throws Exception
048 {
049 // TODO
050 return null;
051 }
052
053 /** Callback from the BaseMBean to notify that an attribute has changed.
054 * Can be used to implement persistence.
055 *
056 * @param oname
057 * @param name
058 * @param value
059 */
060 public void updateField( ObjectName oname, String name,
061 Object value ) {
062 // nothing by default
063 }
064
065 public void store() {
066 // nothing
067 }
068
069 protected InputStream getInputStream() throws IOException {
070 if( source instanceof URL ) {
071 URL url=(URL)source;
072 location=url.toString();
073 return url.openStream();
074 } else if( source instanceof File ) {
075 location=((File)source).getAbsolutePath();
076 return new FileInputStream((File)source);
077 } else if( source instanceof String) {
078 location=(String)source;
079 return new FileInputStream((String)source);
080 } else if( source instanceof InputStream ) {
081 return (InputStream)source;
082 }
083 return null;
084 }
085
086 }