001 /*****************************************************************************
002 * Copyright (C) NanoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 *****************************************************************************/
009
010 package org.nanocontainer.script;
011
012 import org.picocontainer.Parameter;
013 import org.nanocontainer.ClassNameKey;
014 import org.nanocontainer.NanoContainer;
015
016
017 public class ComponentElementHelper {
018
019 public static void makeComponent(Object cnkey, Object key, Parameter[] parameters, Object klass, NanoContainer current, Object instance) {
020 if (cnkey != null) {
021 key = new ClassNameKey((String)cnkey);
022 }
023
024 if (klass instanceof Class) {
025 Class clazz = (Class) klass;
026 key = key == null ? clazz : key;
027 current.getPico().registerComponentImplementation(key, clazz, parameters);
028 } else if (klass instanceof String) {
029 String className = (String) klass;
030 key = key == null ? className : key;
031 try {
032 current.registerComponentImplementation(key, className, parameters);
033 } catch (ClassNotFoundException e) {
034 throw new NanoContainerMarkupException("ClassNotFoundException: " + e.getMessage(), e);
035 }
036 } else if (instance != null) {
037 key = key == null ? instance.getClass() : key;
038 current.getPico().registerComponentInstance(key, instance);
039 } else {
040 throw new NanoContainerMarkupException("Must specify a 'class' attribute for a component as a class name (string) or Class.");
041 }
042 }
043
044
045 }