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.cocoon.woody.binding;
018:
019: import java.lang.reflect.Method;
020:
021: import org.apache.avalon.framework.CascadingRuntimeException;
022: import org.apache.cocoon.woody.formmodel.Widget;
023: import org.apache.commons.jxpath.JXPathContext;
024:
025: /**
026: * InsertBeanJXPathBinding provides an implementation of a {@link Binding}
027: * that inserts a new instance of the specified bean (classname) into the target
028: * back-end model upon save.
029: * <p>
030: * NOTES: <ol>
031: * <li>This Binding does not perform any actions when loading.</li>
032: * </ol>
033: *
034: * @version CVS $Id: InsertBeanJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class InsertBeanJXPathBinding extends JXPathBindingBase {
037:
038: private final String className;
039: private final String addMethodName;
040:
041: /**
042: * Constructs InsertBeanJXPathBinding
043: */
044: public InsertBeanJXPathBinding(
045: JXPathBindingBuilderBase.CommonAttributes commonAtts,
046: String className, String addMethod) {
047: super (commonAtts);
048: this .className = className;
049: this .addMethodName = addMethod;
050: }
051:
052: /**
053: * Do-nothing implementation of the interface.
054: */
055: public void doLoad(Widget frmModel, JXPathContext jxpc) {
056: // doesn't do a thing when loading.
057: }
058:
059: /**
060: * Registers a JXPath Factory on the JXPath Context.
061: * <p>
062: * The factory will insert a new instance of the specified bean (classname)
063: * inside this object into the target objectmodel.
064: */
065: public void doSave(Widget frmModel, JXPathContext jxpc)
066: throws BindingException {
067: try {
068: Object parent = jxpc.getContextBean();
069: Object[] args = new Object[1];
070: Class[] argTypes = new Class[1];
071:
072: // instantiate the new object
073: argTypes[0] = Class.forName(this .className);
074: args[0] = argTypes[0].newInstance();
075:
076: // lookup the named method on the parent
077: Method addMethod = parent.getClass().getMethod(
078: this .addMethodName, argTypes);
079:
080: // invoke this method with this new beast.
081: addMethod.invoke(parent, args);
082:
083: if (getLogger().isDebugEnabled())
084: getLogger().debug("InsertBean performed.");
085: } catch (Exception e) {
086: throw new CascadingRuntimeException("InsertBean failed.", e);
087: }
088:
089: // jxpc.setFactory(new AbstractFactory() {
090: // public boolean createObject(JXPathContext context, Pointer pointer,
091: // Object parent, String name, int index) {
092: // try {
093: // Object[] args = new Object[1];
094: // Class[] argTypes = new Class[1];
095: //
096: // // instantiate the new object
097: // argTypes[0] = Class.forName(InsertBeanJXPathBinding.this.className);
098: // args[0] = argTypes[0].newInstance();
099: // // lookup the named method on the parent
100: //
101: // Method addMethod =
102: // parent.getClass().getMethod(InsertBeanJXPathBinding.this.addMethodName, argTypes);
103: // // invoke this method with this new beast.
104: //
105: // addMethod.invoke(parent, args);
106: //
107: // if (getLogger().isDebugEnabled())
108: // getLogger().debug("InsertBean jxpath factory executed for index " + index);
109: // return true;
110: // } catch (Exception e) {
111: // throw new CascadingRuntimeException("InsertBean jxpath factory failed.", e);
112: // }
113: // }
114: // });
115: //
116: // if (getLogger().isDebugEnabled())
117: // getLogger().debug("done registered factory for inserting node -- " + toString());
118: }
119:
120: public String toString() {
121: return "InsertBeanJXPathBinding [for class " + this .className
122: + " to addMethod " + this .addMethodName + "]";
123: }
124:
125: }
|