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.xbean.server.loader; 018 019 import java.util.List; 020 import java.util.Iterator; 021 022 import org.apache.xbean.server.main.Main; 023 import org.apache.xbean.server.main.FatalStartupError; 024 import org.apache.xbean.kernel.Kernel; 025 import org.apache.xbean.kernel.ServiceName; 026 027 /** 028 * LoadAllMain loads all configurations specified in the arguments passed to main. 029 * 030 * @org.apache.xbean.XBean namespace="http://xbean.apache.org/schemas/server" element="load-all-main" 031 * description="Loads all configurations specified in the arguments passed to main" 032 * 033 * @author Dain Sundstrom 034 * @version $Id$ 035 * @since 2.0 036 */ 037 public class LoadAllMain implements Main { 038 private Kernel kernel; 039 private Main next; 040 041 /** 042 * Gets the kernel in which configurations are loaded. 043 * @return the kernel in which configurations are loaded 044 */ 045 public Kernel getKernel() { 046 return kernel; 047 } 048 049 /** 050 * Sets the kernel in which configurations are loaded. 051 * @param kernel the kernel in which configurations are loaded 052 */ 053 public void setKernel(Kernel kernel) { 054 this.kernel = kernel; 055 } 056 057 /** 058 * Gets the next main instance to call. 059 * @return the next main instance to call 060 */ 061 public Main getNext() { 062 return next; 063 } 064 065 /** 066 * Sets the next main instance to call. 067 * @param next the next main instance to call 068 */ 069 public void setNext(Main next) { 070 this.next = next; 071 } 072 073 /** 074 * Loads all configurations specified in the args and call the next main instance with no arguments. 075 * @param args the configurations to load 076 */ 077 public void main(String[] args) { 078 for (int i = 0; i < args.length; i++) { 079 String location = args[i]; 080 load(location); 081 } 082 083 if (next != null) { 084 next.main(new String[0]); 085 } 086 } 087 088 private void load(String location) { 089 List loaders = kernel.getServices(Loader.class); 090 for (Iterator iterator = loaders.iterator(); iterator.hasNext();) { 091 Loader loader = (Loader) iterator.next(); 092 try { 093 ServiceName configurationName = loader.load(location); 094 if (configurationName != null) { 095 kernel.startServiceRecursive(configurationName); 096 return; 097 } 098 } catch (Exception e) { 099 throw new FatalStartupError("Error loading '" + location + "' with " + loader, e); 100 } 101 } 102 String message = "No loaders were able to load '" + location + "' : Available loaders "; 103 for (Iterator iterator = loaders.iterator(); iterator.hasNext();) { 104 message += iterator.next(); 105 if (iterator.hasNext()) { 106 message += ", "; 107 } 108 } 109 throw new FatalStartupError(message); 110 } 111 }