001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/mgt/factories/FieldValueWrapperFactoryImpl.java $
003: * $Id: FieldValueWrapperFactoryImpl.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.mgt.factories;
021:
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.metaobj.shared.mgt.FieldValueWrapperFactory;
030: import org.sakaiproject.metaobj.utils.mvc.intf.FieldValueWrapper;
031:
032: /**
033: * Created by IntelliJ IDEA.
034: * User: John Ellis
035: * Date: Apr 25, 2004
036: * Time: 6:48:28 PM
037: * To change this template use File | Settings | File Templates.
038: */
039: public class FieldValueWrapperFactoryImpl implements
040: FieldValueWrapperFactory {
041:
042: private Map wrappedClassMap = null;
043: protected final Log logger = LogFactory.getLog(getClass());
044:
045: public FieldValueWrapperFactoryImpl() {
046: }
047:
048: public boolean checkWrapper(Class clazz) {
049: return (wrappedClassMap.get(clazz) != null);
050: }
051:
052: public FieldValueWrapper wrapInstance(Class clazz) {
053: Class wrapperClass = (Class) wrappedClassMap.get(clazz);
054:
055: FieldValueWrapper returnedWrapper = null;
056:
057: try {
058: returnedWrapper = (FieldValueWrapper) wrapperClass
059: .newInstance();
060: } catch (InstantiationException e) {
061: RuntimeException exp = new IllegalArgumentException(
062: "Invalid wrapper class");
063: exp.initCause(e);
064: throw exp;
065: } catch (IllegalAccessException e) {
066: RuntimeException exp = new IllegalArgumentException(
067: "Invalid wrapper class");
068: exp.initCause(e);
069: throw exp;
070: }
071:
072: return returnedWrapper;
073: }
074:
075: public FieldValueWrapper wrapInstance(Object obj) {
076:
077: FieldValueWrapper returnedWrapper = wrapInstance(obj.getClass());
078:
079: returnedWrapper.setValue(obj);
080:
081: return returnedWrapper;
082: }
083:
084: public void setWrappedClassMap(Map wrappedClassMap) {
085: this .wrappedClassMap = wrappedClassMap;
086: }
087:
088: public void setWrappedClassNamesMap(Properties wrappedClassNamesMap)
089: throws ClassNotFoundException {
090: wrappedClassMap = new Hashtable();
091:
092: for (Iterator i = wrappedClassNamesMap.keySet().iterator(); i
093: .hasNext();) {
094: String key = (String) i.next();
095: String value = wrappedClassNamesMap.getProperty(key);
096:
097: wrappedClassMap.put(Class.forName(key), Class
098: .forName(value));
099: }
100:
101: setWrappedClassMap(wrappedClassMap);
102: }
103: }
|