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: */package org.apache.geronimo.connector.deployment.jsr88;
017:
018: import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
019: import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
020: import org.apache.xmlbeans.XmlObject;
021:
022: import javax.enterprise.deploy.model.XpathListener;
023: import javax.enterprise.deploy.model.XpathEvent;
024: import javax.enterprise.deploy.model.DDBean;
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.util.Set;
028: import java.util.HashSet;
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.util.Iterator;
032:
033: /**
034: * Base class for beans that hold an array of config property settings.
035: *
036: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
037: */
038: public abstract class ConfigHolder extends XmlBeanSupport {
039: final XpathListener xpathListener = new XpathListener() {
040: public void fireXpathEvent(XpathEvent event) {
041: if (event.isAddEvent()) {
042: //todo: add new config-property-setting, fire change event
043: } else if (event.isRemoveEvent()) {
044: //todo: remove config-property-setting, fire change event
045: } else if (event.isChangeEvent()) {
046: if (event.getChangeEvent().getPropertyName().equals(
047: "config-property-name")) {
048: String old = (String) event.getChangeEvent()
049: .getOldValue();
050: for (int i = 0; i < settings.length; i++) {
051: ConfigPropertySetting setting = settings[i];
052: if (setting.getName().equals(old)) {
053: setting.setName((String) event
054: .getChangeEvent().getNewValue());
055: break;
056: }
057: }
058: }
059: }
060: }
061: };
062: private DDBean ddBean;
063: private ConfigPropertySetting[] settings = new ConfigPropertySetting[0];
064:
065: public ConfigHolder() {
066: super (null);
067: }
068:
069: public void clearNullSettings() {
070: List list = new ArrayList();
071: Set saved = new HashSet();
072: for (int i = 0; i < settings.length; i++) {
073: ConfigPropertySetting setting = settings[i];
074: if (setting.getValue() != null && !setting.isSetToDefault()) {
075: list.add(setting);
076: saved.add(setting.getName());
077: }
078: }
079: settings = (ConfigPropertySetting[]) list
080: .toArray(new ConfigPropertySetting[list.size()]);
081: GerConfigPropertySettingType[] configs = getConfigProperties();
082: for (int i = configs.length - 1; i >= 0; --i) {
083: GerConfigPropertySettingType type = configs[i];
084: if (!saved.contains(type.getName())) {
085: removeConfigProperty(i);
086: }
087: }
088: }
089:
090: protected void configure(DDBean ddBean, XmlObject xml) {
091: ConfigPropertySetting[] old = null;
092: if (this .ddBean != null) {
093: this .ddBean.removeXpathListener("config-property",
094: xpathListener);
095: old = settings;
096: }
097: this .ddBean = ddBean;
098: setXmlObject(xml);
099:
100: // Prepare the ConfigPropertySetting array
101: List list = new ArrayList();
102: DDBean[] all = ddBean == null ? new DDBean[0] : ddBean
103: .getChildBean("config-property");
104: if (all == null) {
105: all = new DDBean[0];
106: }
107: Map byName = new HashMap();
108: for (int i = 0; i < all.length; i++) {
109: DDBean item = all[i];
110: byName.put(item.getText("config-property-name")[0], item);
111: }
112: GerConfigPropertySettingType[] previous = getConfigProperties();
113: for (int i = 0; i < previous.length; i++) {
114: GerConfigPropertySettingType setting = previous[i];
115: DDBean item = (DDBean) byName.remove(setting.getName());
116: if (item != null) {
117: list
118: .add(new ConfigPropertySetting(item, setting,
119: false));
120: } else {
121: System.out
122: .println("Ignoring connectiondefinition-instance/config-setting "
123: + setting.getName()
124: + " (no matching config-property in J2EE DD)");
125: //todo: delete it from the XMLBeans tree
126: }
127: }
128: for (Iterator it = byName.keySet().iterator(); it.hasNext();) {
129: String name = (String) it.next();
130: DDBean bean = (DDBean) byName.get(name);
131: list.add(new ConfigPropertySetting(bean,
132: createConfigProperty(), true));
133: }
134: settings = (ConfigPropertySetting[]) list
135: .toArray(new ConfigPropertySetting[list.size()]);
136: if (old != null) {
137: pcs.firePropertyChange("configPropertySetting", old,
138: settings);
139: }
140: if (ddBean != null) {
141: ddBean.addXpathListener("config-property", xpathListener);
142: }
143: }
144:
145: public ConfigPropertySetting[] getConfigPropertySetting() {
146: return settings;
147: }
148:
149: public ConfigPropertySetting getConfigPropertySetting(int index) {
150: return settings[index];
151: }
152:
153: protected abstract GerConfigPropertySettingType createConfigProperty();
154:
155: protected abstract GerConfigPropertySettingType[] getConfigProperties();
156:
157: protected abstract void removeConfigProperty(int index);
158:
159: public abstract void reconfigure();
160: }
|