KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config;
30
31 import com.caucho.util.L10N;
32 import com.caucho.xml.QName;
33
34 import org.w3c.dom.Node JavaDoc;
35
36 import java.lang.reflect.Method JavaDoc;
37
38 public class CreateAttributeStrategy extends AttributeStrategy {
39   static final L10N L = new L10N(CreateAttributeStrategy.class);
40
41   private Method JavaDoc _createMethod;
42   private Method JavaDoc _setterMethod;
43
44   public CreateAttributeStrategy(Method JavaDoc createMethod, Method JavaDoc setter)
45   {
46     _createMethod = createMethod;
47     _setterMethod = setter;
48   }
49
50   /**
51    * Gets the attribute's method
52    */

53   public Method JavaDoc getCreateMethod()
54   {
55     return _createMethod;
56   }
57
58   /**
59    * Configures the attribute with the given configuration node.
60    *
61    * @param builder the node builder context
62    * @param bean the bean to be configured
63    * @param name the attribute name
64    * @param node the configuration node
65    * @throws Exception
66    */

67
68   public void configure(NodeBuilder builder,
69                         Object JavaDoc bean,
70                         QName name,
71                         Node JavaDoc node)
72           throws Exception JavaDoc
73   {
74     // server/23j0
75
Object JavaDoc child = builder.createResinType(node);
76
77     if (child == null)
78       child = _createMethod.invoke(bean);
79
80     TypeStrategy childStrategy;
81
82     childStrategy = TypeStrategyFactory.getTypeStrategy(child.getClass());
83
84     child = builder.configureImpl(childStrategy, child, node);
85
86     setChild(bean, name, child);
87   }
88
89   /**
90    * Creates the child instance.
91    */

92   public Object JavaDoc create(Object JavaDoc parent)
93     throws Exception JavaDoc
94   {
95     return _createMethod.invoke(parent, new Object JavaDoc[0]);
96   }
97
98   /**
99    * Sets the child object.
100    */

101   public void setChild(Object JavaDoc bean, QName name, Object JavaDoc child)
102     throws Exception JavaDoc
103   {
104     if (_setterMethod != null) {
105       try {
106         _setterMethod.invoke(bean, new Object JavaDoc[] { child });
107       } catch (IllegalArgumentException JavaDoc e) {
108         throw new IllegalArgumentException JavaDoc(_setterMethod.getName() + ": " + e);
109       }
110     }
111   }
112 }
113
Popular Tags