KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > MapBuilder


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  */

27
28 package com.caucho.config;
29
30 import com.caucho.log.Log;
31 import com.caucho.util.L10N;
32 import com.caucho.xml.QName;
33
34 import java.util.Map JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * MapBuilder populates the bean based on a map.
39  */

40 public class MapBuilder {
41   private final static L10N L = new L10N(MapBuilder.class);
42   private final static Logger JavaDoc log = Log.open(MapBuilder.class);
43   
44   /**
45    * Configures the bean from a path
46    */

47   public static Object JavaDoc configure(Object JavaDoc bean, Map JavaDoc<String JavaDoc,Object JavaDoc> map)
48     throws ConfigException
49   {
50     return configure(bean, map, true);
51   }
52   
53   /**
54    * Configures the bean from a path
55    */

56   public static Object JavaDoc configureNoInit(Object JavaDoc bean, Map JavaDoc<String JavaDoc,Object JavaDoc> map)
57     throws ConfigException
58   {
59     return configure(bean, map, false);
60   }
61   
62   /**
63    * The first cut should just set attributes based
64    * on the RegistryNode, i.e. basically cut and paste
65    * from BeanUtil.
66    *
67    * @param bean NOT NULL
68    *
69    * @todo How to handle &lt;foo/&gt;
70    */

71   public static Object JavaDoc configure(Object JavaDoc bean,
72                  Map JavaDoc<String JavaDoc,Object JavaDoc> map,
73                  boolean doInit)
74     throws ConfigException
75   {
76     NodeBuilder oldBuilder = NodeBuilder.getCurrentBuilder();
77
78     try {
79       if (oldBuilder == null)
80     NodeBuilder.setCurrentBuilder(new NodeBuilder());
81       
82       TypeStrategy type = TypeStrategyFactory.getTypeStrategy(bean.getClass());
83
84       return configure(type, bean, map, doInit);
85     } catch (ConfigException e) {
86       throw e;
87     } catch (RuntimeException JavaDoc e) {
88       throw e;
89     } catch (Throwable JavaDoc e) {
90       throw new ConfigException(e);
91     } finally {
92       NodeBuilder.setCurrentBuilder(oldBuilder);
93     }
94   }
95
96   private static Object JavaDoc configure(TypeStrategy typeStrategy,
97                  Object JavaDoc bean,
98                  Map JavaDoc<String JavaDoc,Object JavaDoc> map,
99                  boolean doInit)
100     throws Throwable JavaDoc
101   {
102     /*
103     // XXX: make common
104     if (bean instanceof EnvironmentBean) {
105       EnvironmentBean envBean = (EnvironmentBean) bean;
106
107       ClassLoader beanLoader = envBean.getClassLoader();
108
109       if (beanLoader != null) {
110         thread.setContextClassLoader(beanLoader);
111     factory.getConfigVariableResolver().setConfigLoader(beanLoader);
112       }
113     }
114     */

115
116     for (String JavaDoc key : map.keySet()) {
117       QName attrName = new QName(key);
118       
119       AttributeStrategy attrStrategy
120     = typeStrategy.getAttributeStrategy(attrName);
121
122       if (attrStrategy != null)
123     attrStrategy.setAttribute(bean, attrName, map.get(key));
124     }
125
126     if (doInit) {
127       typeStrategy.init(bean);
128
129       return typeStrategy.replaceObject(bean);
130     }
131     else
132       return bean;
133   }
134 }
135
Popular Tags