KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > util > PropertyLoader


1 /*
2
3  * Copyright 1999-2004 The Apache Software Foundation.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  * http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  *
30
31  */

32
33
34 package org.apache.sandesha.util;
35
36
37 import org.apache.axis.components.logger.LogFactory;
38 import org.apache.commons.logging.Log;
39 import org.apache.sandesha.Constants;
40
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.util.*;
44
45 /**
46  * This is the property loader for Sandesha. All the properties will be loaded from the
47  * <p/>
48  * sandesha.properties file that is found in the classpath.
49  *
50  * @author Jaliya Ekanayake
51  * @author Patrick Collins
52  */

53
54 public class PropertyLoader {
55     private static final Log log = LogFactory.getLog(PropertyLoader.class.getName());
56
57     public static int getClientSideListenerPort() {
58         return getIntProperty(Constants.ClientProperties.CLIENT_LISTENER_PORT,
59                 Constants.DEFAULR_CLIENT_SIDE_LISTENER_PORT);
60     }
61
62     public static int getSimpleAxisServerPort() {
63         return getIntProperty(Constants.ClientProperties.SIMPLE_AXIS_SERVER_PORT_POPERTY,
64                 Constants.DEFAULT_SIMPLE_AXIS_SERVER_PORT);
65     }
66
67     protected static int getIntProperty(String JavaDoc aKey, int aValue) {
68         int retVal = aValue;
69         String JavaDoc intValue = getStringProperty(aKey, String.valueOf(aValue));
70         try {
71             retVal = Integer.parseInt(intValue);
72         } catch (NumberFormatException JavaDoc nfe) {
73
74         }
75         return retVal;
76
77     }
78
79     public static ArrayList getRequestHandlerNames() {
80         return getHandlerNames(Constants.ClientProperties.REQUEST_HANDLER);
81     }
82
83     public static ArrayList getResponseHandlerNames() {
84         return getHandlerNames(Constants.ClientProperties.RESPONSE_HANDLER);
85     }
86
87     public static ArrayList getHandlerNames(String JavaDoc type) {
88         Properties properties = new Properties();
89         load(properties);
90         ArrayList ret = new ArrayList();
91         int temp = 0;
92         String JavaDoc propVal;
93         do {
94             temp++;
95             String JavaDoc tempStr = type + temp;
96             propVal = properties.getProperty(tempStr);
97             if (propVal != null) {
98                 ret.add(propVal);
99             }
100
101         } while (propVal != null);
102         return ret;
103     }
104
105
106     public static ArrayList getListenerRequestHandlerNames() {
107         return getHandlerNames(Constants.ClientProperties.LISTENER_REQUEST_HANDLER);
108     }
109
110     public static ArrayList getListenerResponseHandlerNames() {
111         return getHandlerNames(Constants.ClientProperties.LISTENER_RESPONSE_HANDLER);
112     }
113
114     public static String JavaDoc getProvider() {
115         return getStringProperty(Constants.ClientProperties.PROVIDER_CLASS,
116                 Constants.ClientProperties.DEFAULT_PROVIDER_CLASS);
117     }
118
119     public static String JavaDoc getInvokeStrategyClassName() {
120         String JavaDoc invokeStrategyProperty = getStringProperty(Constants.INVOKE_STRATEGY, Constants.DEFAULT_STRATEGY);
121         String JavaDoc[] splitData = invokeStrategyProperty.split(":");
122         return splitData[0];
123     }
124
125     public static Map getInvokeStrategyParams() {
126         String JavaDoc invokeStrategyProperty = getStringProperty(Constants.INVOKE_STRATEGY, Constants.DEFAULT_STRATEGY);
127         return getParamData(invokeStrategyProperty);
128     }
129
130
131     protected static Map getParamData(String JavaDoc aRawString) {
132         Map params = new HashMap();
133         String JavaDoc[] splitData = aRawString.split(Constants.COLON);
134         if (splitData.length == 2) {
135             addParams(params, splitData[1]);
136         }
137         return params;
138     }
139
140     public static String JavaDoc getInvokeHandlerClassName() {
141         String JavaDoc invokeHandlerProperty = getStringProperty(Constants.INVOKE_HANDLER, Constants.DEFAULT_HANDLER);
142         String JavaDoc[] splitData = invokeHandlerProperty.split(Constants.COLON);
143         return splitData[0];
144     }
145
146     public static Map getInvokeHandlerParams() {
147         String JavaDoc invokeHandlerProperty = getStringProperty(Constants.INVOKE_HANDLER, Constants.DEFAULT_HANDLER);
148         return getParamData(invokeHandlerProperty);
149     }
150
151     protected static void addParams(Map aParams, String JavaDoc aParamString) {
152         StringTokenizer st = new StringTokenizer(aParamString, "&");
153         while (st.hasMoreTokens()) {
154             String JavaDoc nameValuePair = st.nextToken();
155             String JavaDoc[] nameValueArray = nameValuePair.split("=");
156             if (nameValueArray.length == 2) {
157                 aParams.put(nameValueArray[0], nameValueArray[1]);
158             }
159         }
160     }
161
162
163     protected static String JavaDoc getStringProperty(String JavaDoc aKey, String JavaDoc aDefault) {
164         Properties props = new Properties();
165         load(props);
166         return props.getProperty(aKey, aDefault);
167     }
168
169
170     private static void load(Properties aProps) {
171         try {
172             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.ClientProperties.PROPERTY_FILE);
173             aProps.load(in);
174         } catch (IOException JavaDoc e) {
175             log.error(e);
176         }
177     }
178 }
179
180
Popular Tags