Java tutorial
/** * Copyright (C) 2012 Smithsonian Astrophysical Observatory * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cfa.vo.iris; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.lang.StringUtils; public class ComponentLoader { protected List<IrisComponent> components = new ArrayList<>(); protected List<String> failures = new ArrayList<>(); public ComponentLoader(Collection<Class<? extends IrisComponent>> componentList) { for (Class c : componentList) { loadComponent(c); } } public ComponentLoader(URL componentsURL) { try { initComponents(readComponentsFile(componentsURL)); } catch (IOException ex) { String message = "Cannot read components file at: " + componentsURL; System.err.println(message); Logger.getLogger(ComponentLoader.class.getName()).log(Level.SEVERE, message, ex); } } public List<IrisComponent> getComponents() { return components; } /** * @return list of components to load * @throws IOException * for issues related to reading componentsURL. */ private List<String> readComponentsFile(URL componentsURL) throws IOException { if (componentsURL == null) { throw new IOException("No components file specified!"); } InputStream is = componentsURL.openStream(); BufferedReader r = new BufferedReader(new InputStreamReader(is)); List<String> componentsList = new LinkedList<String>(); String line; while (!StringUtils.isEmpty(line = r.readLine())) { componentsList.add(line); } r.close(); return componentsList; } public final void loadComponent(Class<? extends IrisComponent> componentClass) { try { Logger.getLogger(ComponentLoader.class.getName()).log(Level.INFO, "Loading class: " + componentClass.getName()); IrisComponent component = componentClass.newInstance(); components.add(component); } catch (Exception ex) { String message = "Could not construct component " + componentClass.getName(); System.err.println(message); Logger.getLogger(ComponentLoader.class.getName()).log(Level.SEVERE, message, ex); failures.add(componentClass.getName()); } } /** * Tries to find and construct the classes enumerated in the components list. Components that cannot * be constructed will be logged and skipped. */ private void initComponents(List<String> componentsList) { for (String className : componentsList) { try { Class clazz = Class.forName(className); loadComponent(clazz); } catch (ClassNotFoundException ex) { String message = "cannot find class " + className; System.err.println(message); Logger.getLogger(ComponentLoader.class.getName()).log(Level.SEVERE, message, ex); failures.add(className); } } } }