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.recipe;
018    
019    import org.apache.xbean.propertyeditor.PropertyEditors;
020    import org.apache.xbean.propertyeditor.PropertyEditorException;
021    
022    
023    /**
024     * @version $Rev: 6689 $ $Date: 2006-01-02T06:48:49.815187Z $
025     */
026    public class ValueRecipe extends AbstractRecipe {
027        private final String type;
028        private final String value;
029    
030        public ValueRecipe(Class type, String value) {
031            if (type == null) throw new NullPointerException("type is null");
032            if (!PropertyEditors.canConvert(type)) {
033                throw new IllegalArgumentException("No converter available for " + type.getSimpleName());
034            }
035            this.type = type.getName();
036            this.value = value;
037        }
038    
039        public ValueRecipe(String type, String value) {
040            if (type == null) throw new NullPointerException("type is null");
041            this.type = type;
042            this.value = value;
043        }
044    
045        public ValueRecipe(Object value) {
046            if (value == null) throw new NullPointerException("value is null");
047            this.type = value.getClass().getName();
048            this.value = PropertyEditors.toString(value);
049        }
050    
051        public ValueRecipe(ValueRecipe valueRecipe) {
052            if (valueRecipe == null) throw new NullPointerException("valueRecipe is null");
053            this.type = valueRecipe.type;
054            this.value = valueRecipe.value;
055        }
056    
057        public boolean canCreate(Class type, ClassLoader classLoader) {
058            try {
059                Class myType = Class.forName(this.type, true, classLoader);
060                return type.isAssignableFrom(myType);
061            } catch (ClassNotFoundException e) {
062                throw new ConstructionException("Type class could not be found: " + type);
063            }
064        }
065    
066        public String getType() {
067            return type;
068        }
069    
070        public String getValue() {
071            return value;
072        }
073    
074        public Object create(ClassLoader classLoader) {
075            if (value == null) {
076                return null;
077            }
078    
079            try {
080                return PropertyEditors.getValue(type, value, classLoader);
081            } catch (PropertyEditorException e) {
082                throw new ConstructionException(e);
083            }
084        }
085    }