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: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping;
022: import org.apache.beehive.netui.util.logging.Logger;
023: import org.apache.struts.action.ActionMapping;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import java.lang.reflect.Method;
027: import java.lang.reflect.InvocationTargetException;
028:
029: public class XmlBeanActionForm extends AnyBeanActionForm {
030: private static final Logger _log = Logger
031: .getInstance(XmlBeanActionForm.class);
032:
033: private String _formClassName;
034:
035: public XmlBeanActionForm() {
036: }
037:
038: public XmlBeanActionForm(Object xml) {
039: setBean(xml);
040: }
041:
042: public String getXmlString() {
043: Object xmlBean = getBean();
044:
045: if (xmlBean == null)
046: return null;
047:
048: try {
049: return (String) xmlBean.getClass().getMethod("xmlText",
050: null).invoke(xmlBean, null);
051: } catch (InvocationTargetException e) {
052: _log.error("Error while getting XML String", e.getCause());
053: } catch (Exception e) {
054: assert e instanceof NoSuchMethodException
055: || e instanceof IllegalAccessException : e
056: .getClass().getName();
057: _log.error("Error while getting XML String", e);
058: }
059:
060: return null;
061: }
062:
063: public void setXmlString(String xml) {
064: assert _formClassName != null;
065: setBean(invokeFactoryMethod("parse",
066: new Class[] { String.class }, new Object[] { xml }));
067: }
068:
069: public void reset(ActionMapping mapping, HttpServletRequest request) {
070: if (_formClassName == null) {
071: assert mapping instanceof PageFlowActionMapping : mapping
072: .getClass().getName();
073: _formClassName = ((PageFlowActionMapping) mapping)
074: .getFormClass();
075: assert _formClassName != null;
076: }
077:
078: if (getBean() == null) {
079: setBean(invokeFactoryMethod("newInstance", new Class[0],
080: new Object[0]));
081: }
082: }
083:
084: private Object invokeFactoryMethod(String methodName,
085: Class[] argTypes, Object[] args) {
086: String factoryClassName = _formClassName + "$Factory";
087:
088: try {
089: Class factoryClass = Class.forName(factoryClassName);
090: Method newInstanceMethod = factoryClass.getMethod(
091: methodName, argTypes);
092: return newInstanceMethod.invoke(factoryClass, args);
093: } catch (Exception e) {
094: // Can be any exception -- not just the reflection-related exceptions...
095: // because the exception could be thrown while creating the XML bean.
096: if (_log.isErrorEnabled()) {
097: _log.error("Error while creating XML object of type "
098: + _formClassName, e);
099: }
100:
101: return null;
102: }
103: }
104: }
|