001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.eventops;
019:
020: import java.net.*;
021: import java.util.*;
022: import javax.servlet.*;
023:
024: import org.w3c.dom.*;
025: import org.ofbiz.base.util.*;
026: import org.ofbiz.minilang.*;
027: import org.ofbiz.minilang.method.*;
028:
029: /**
030: * Copies a property value from a properties file in a ServletContext resource to a field
031: */
032: public class WebappPropertyToField extends MethodOperation {
033:
034: public static final String module = WebappPropertyToField.class
035: .getName();
036:
037: String resource;
038: String property;
039: String defaultVal;
040: ContextAccessor mapAcsr;
041: ContextAccessor fieldAcsr;
042:
043: public WebappPropertyToField(Element element,
044: SimpleMethod simpleMethod) {
045: super (element, simpleMethod);
046: resource = element.getAttribute("resource");
047: property = element.getAttribute("property");
048: defaultVal = element.getAttribute("default");
049: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
050: fieldAcsr = new ContextAccessor(element
051: .getAttribute("field-name"));
052: }
053:
054: public boolean exec(MethodContext methodContext) {
055: String resource = methodContext.expandString(this .resource);
056: String property = methodContext.expandString(this .property);
057: String defaultVal = methodContext.expandString(this .defaultVal);
058:
059: String fieldVal = null;
060:
061: // only run this if it is in an EVENT context
062: if (methodContext.getMethodType() == MethodContext.EVENT) {
063: ServletContext servletContext = (ServletContext) methodContext
064: .getRequest().getAttribute("servletContext");
065: URL propsUrl = null;
066:
067: try {
068: propsUrl = servletContext.getResource(resource);
069: } catch (java.net.MalformedURLException e) {
070: Debug.logWarning(e,
071: "Error finding webapp resource (properties file) not found with name "
072: + resource, module);
073: }
074:
075: if (propsUrl == null) {
076: Debug.logWarning(
077: "Webapp resource (properties file) not found with name "
078: + resource, module);
079: } else {
080: fieldVal = UtilProperties.getPropertyValue(propsUrl,
081: property);
082: if (fieldVal == null || fieldVal.length() == 0) {
083: Debug.logWarning(
084: "Webapp resource property value not found with name "
085: + property + " in resource "
086: + resource, module);
087: }
088: }
089: }
090:
091: // if fieldVal is null, or has zero length, use defaultVal
092: if ((fieldVal == null) || (fieldVal.length() == 0)) {
093: fieldVal = defaultVal;
094: }
095:
096: if (!mapAcsr.isEmpty()) {
097: Map fromMap = (Map) mapAcsr.get(methodContext);
098:
099: if (fromMap == null) {
100: Debug.logWarning("Map not found with name " + mapAcsr
101: + " creating a new map", module);
102: fromMap = new HashMap();
103: mapAcsr.put(methodContext, fromMap);
104: }
105:
106: fieldAcsr.put(fromMap, fieldVal, methodContext);
107: } else {
108: fieldAcsr.put(methodContext, fieldVal);
109: }
110: return true;
111: }
112:
113: public String rawString() {
114: // TODO: add all attributes and other info
115: return "<webapp-property-to-field field-name=\""
116: + this .fieldAcsr + "\" map-name=\"" + this .mapAcsr
117: + "\"/>";
118: }
119:
120: public String expandedString(MethodContext methodContext) {
121: // TODO: something more than a stub/dummy
122: return this.rawString();
123: }
124: }
|