001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.server.config;
023:
024: import org.jboss.portal.common.util.CLResourceLoader;
025: import org.jboss.portal.common.util.LoaderResource;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.TreeMap;
031:
032: /**
033: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
034: * @version $Revision: 8784 $
035: */
036: public class ServerConfigService implements ServerConfig {
037:
038: /** . */
039: private final String domain = "portal";
040:
041: /** . */
042: private String configLocation;
043:
044: /** . */
045: private LoaderResource configResource;
046:
047: /** . */
048: private Map properties;
049:
050: public String getDomain() {
051: return domain;
052: }
053:
054: public ServerConfigService() {
055: properties = new HashMap();
056: }
057:
058: public String getConfigLocation() {
059: return configLocation;
060: }
061:
062: public void setConfigLocation(String configLocation) {
063: this .configLocation = configLocation;
064: }
065:
066: public LoaderResource getConfigResource() {
067: return configResource;
068: }
069:
070: public String getProperty(String name) {
071: return (String) properties.get(name);
072: }
073:
074: public void setProperty(String name, String value) {
075: if (name == null) {
076: throw new IllegalArgumentException("No null name accepted");
077: }
078: synchronized (this ) {
079: Map copy = new HashMap(properties);
080: if (value != null) {
081: value = value.trim();
082: copy.put(name, value);
083: } else {
084: copy.remove(name);
085: }
086: properties = copy;
087: }
088: }
089:
090: public String dumpProperties(boolean html) {
091: StringBuffer buffer = new StringBuffer();
092: for (Iterator i = new TreeMap(properties).entrySet().iterator(); i
093: .hasNext();) {
094: Map.Entry entry = (Map.Entry) i.next();
095: buffer.append(entry.getKey()).append("=").append(
096: entry.getValue()).append(html ? "<br/>" : "\n");
097: }
098: return buffer.toString();
099: }
100:
101: public void create() throws Exception {
102: configResource = new CLResourceLoader()
103: .getResource(configLocation);
104: if (configResource.exists()) {
105: properties.clear();
106: properties.putAll(configResource.asProperties(true));
107: }
108:
109: //
110: for (Iterator i = properties.entrySet().iterator(); i.hasNext();) {
111: Map.Entry entry = (Map.Entry) i.next();
112: String value = (String) entry.getValue();
113: value = value.trim();
114: entry.setValue(value);
115: }
116:
117: // Add the domain for now portal value
118: properties.put(DOMAIN_KEY, domain);
119: }
120:
121: public void destroy() throws Exception {
122: properties.clear();
123: configResource = null;
124: }
125: }
|