|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ServiceLoader.java | 68.2% | 79.4% | 88.9% | 77.7% |
|
||||||||||||||
| 1 |
/**
|
|
| 2 |
* @(#)ServiceLoader.java
|
|
| 3 |
*
|
|
| 4 |
* JFoxSOAF, Service-Oriented Application Framework
|
|
| 5 |
*
|
|
| 6 |
* Copyright(c) JFoxSOAF Team
|
|
| 7 |
*
|
|
| 8 |
* Licensed under the GNU LGPL, Version 2.1 (the "License");
|
|
| 9 |
* you may not use this file except in compliance with the License.
|
|
| 10 |
* You may obtain a copy of the License at
|
|
| 11 |
*
|
|
| 12 |
* http://www.gnu.org/copyleft/lesser.html
|
|
| 13 |
*
|
|
| 14 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 17 |
* See the License for the specific language governing permissions and
|
|
| 18 |
* limitations under the License.
|
|
| 19 |
*
|
|
| 20 |
* For more information, please visit:
|
|
| 21 |
* http://www.jfox.cn/confluence/display/JFoxSOAF/Home
|
|
| 22 |
* http://www.huihoo.org/jfox/jfoxsoaf
|
|
| 23 |
*/
|
|
| 24 |
|
|
| 25 |
package org.huihoo.jfox.soaf.container;
|
|
| 26 |
|
|
| 27 |
import java.io.IOException;
|
|
| 28 |
import java.io.InputStream;
|
|
| 29 |
import java.io.InputStreamReader;
|
|
| 30 |
import java.util.ArrayList;
|
|
| 31 |
import java.util.Collection;
|
|
| 32 |
import java.util.Iterator;
|
|
| 33 |
import java.util.LinkedList;
|
|
| 34 |
|
|
| 35 |
import org.apache.commons.logging.Log;
|
|
| 36 |
import org.apache.commons.logging.LogFactory;
|
|
| 37 |
import org.exolab.castor.xml.MarshalException;
|
|
| 38 |
import org.exolab.castor.xml.ValidationException;
|
|
| 39 |
import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
|
|
| 40 |
import org.huihoo.jfox.soaf.schema.config.Configuration;
|
|
| 41 |
import org.huihoo.jfox.soaf.schema.config.ConfigurationEntry;
|
|
| 42 |
import org.huihoo.jfox.soaf.schema.config.Interceptor;
|
|
| 43 |
import org.huihoo.jfox.soaf.schema.service.Service;
|
|
| 44 |
import org.huihoo.jfox.soaf.schema.service.ServiceEntry;
|
|
| 45 |
import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
|
|
| 46 |
|
|
| 47 |
/**
|
|
| 48 |
* <p>
|
|
| 49 |
* Service Configration load proxy.
|
|
| 50 |
* </p>
|
|
| 51 |
*
|
|
| 52 |
* @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
|
|
| 53 |
* @version $Revision: 1.3 $ $Date: 2005/05/22 06:46:48 $
|
|
| 54 |
* @version Revision: 1.0
|
|
| 55 |
*/
|
|
| 56 |
|
|
| 57 |
public class ServiceLoader { |
|
| 58 |
|
|
| 59 |
private static ServiceLoader serviceLoader; |
|
| 60 |
|
|
| 61 |
private boolean serviceLoaded = false; |
|
| 62 |
|
|
| 63 |
private Collection serviceSchemaColl = new LinkedList(); |
|
| 64 |
|
|
| 65 |
private Collection interceptorColl = new ArrayList(); |
|
| 66 |
|
|
| 67 |
private Collection serviceEntryColl = new ArrayList(); |
|
| 68 |
|
|
| 69 |
private InterceptorManager interceptorManager = new InterceptorManagerImpl(); |
|
| 70 |
|
|
| 71 |
private final Log logger = LogFactory.getLog(getClass());
|
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* Singleton instance
|
|
| 75 |
* @return
|
|
| 76 |
*/
|
|
| 77 | 4 |
public static ServiceLoader getInstance() { |
| 78 | 4 |
if (serviceLoader == null) { |
| 79 | 1 |
serviceLoader = new ServiceLoader();
|
| 80 |
} |
|
| 81 | 4 |
return serviceLoader;
|
| 82 |
} |
|
| 83 |
|
|
| 84 |
/**
|
|
| 85 |
* Init services.
|
|
| 86 |
*
|
|
| 87 |
* @param configFile
|
|
| 88 |
* @throws ServiceConfigurationException
|
|
| 89 |
*/
|
|
| 90 | 1 |
public synchronized void initService(String configFile) |
| 91 |
throws ServiceConfigurationException {
|
|
| 92 | 1 |
if (!isServiceLoaded()) {
|
| 93 | 1 |
try {
|
| 94 | 1 |
InputStream is = ResourceHelper.getResourceAsStream(configFile); |
| 95 | 1 |
loadConfig(loadServiceConfigration(is)); |
| 96 |
} catch (Throwable e) {
|
|
| 97 | 0 |
throw new ServiceConfigurationException( |
| 98 |
"Init serivce config file jfoxsoaf-config.xml failed "
|
|
| 99 |
+ e.getMessage()); |
|
| 100 |
} |
|
| 101 | 1 |
serviceLoaded = true;
|
| 102 |
} |
|
| 103 |
} |
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* Init services from input stream.
|
|
| 107 |
*
|
|
| 108 |
* @param is
|
|
| 109 |
* @throws ServiceConfigurationException
|
|
| 110 |
*/
|
|
| 111 | 0 |
public synchronized void initService(InputStream is) |
| 112 |
throws ServiceConfigurationException {
|
|
| 113 | 0 |
if (!isServiceLoaded()) {
|
| 114 | 0 |
try {
|
| 115 | 0 |
loadConfig(loadServiceConfigration(is)); |
| 116 |
} catch (Throwable e) {
|
|
| 117 | 0 |
throw new ServiceConfigurationException( |
| 118 |
"Init serivce config file jfoxsoaf-config.xml failed "
|
|
| 119 |
+ e.getMessage()); |
|
| 120 |
} |
|
| 121 | 0 |
serviceLoaded = true;
|
| 122 |
} |
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/**
|
|
| 126 |
* Load service configration file.
|
|
| 127 |
*
|
|
| 128 |
* @return Service
|
|
| 129 |
* @throws Throwable
|
|
| 130 |
*/
|
|
| 131 | 1 |
private Configuration loadServiceConfigration(InputStream is)
|
| 132 |
throws ServiceConfigurationException {
|
|
| 133 | 1 |
InputStreamReader isr = null;
|
| 134 | 1 |
Configuration config = null;
|
| 135 | 1 |
try {
|
| 136 | 1 |
isr = new InputStreamReader(is);
|
| 137 | 1 |
config = Configuration.unmarshal(isr); |
| 138 |
} catch (MarshalException e) {
|
|
| 139 | 0 |
throw new ServiceConfigurationException(e); |
| 140 |
} catch (ValidationException e) {
|
|
| 141 | 0 |
throw new ServiceConfigurationException(e); |
| 142 |
} finally {
|
|
| 143 | 1 |
if (is != null) { |
| 144 | 1 |
try {
|
| 145 | 1 |
is.close(); |
| 146 |
} catch (IOException e) {
|
|
| 147 | 0 |
e.printStackTrace(); |
| 148 |
} |
|
| 149 |
} |
|
| 150 |
|
|
| 151 | 1 |
if (isr != null) { |
| 152 | 1 |
try {
|
| 153 | 1 |
isr.close(); |
| 154 |
} catch (IOException e) {
|
|
| 155 | 0 |
e.printStackTrace(); |
| 156 |
} |
|
| 157 |
} |
|
| 158 |
} |
|
| 159 | 1 |
return config;
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Load all configuration service.
|
|
| 164 |
*
|
|
| 165 |
* @param service
|
|
| 166 |
*/
|
|
| 167 | 1 |
public void loadConfig(Configuration config) throws Throwable { |
| 168 | 1 |
processConfig(config); |
| 169 | 1 |
loadService(serviceSchemaColl); |
| 170 | 1 |
interceptorManager |
| 171 |
.processInterceptor(interceptorColl, serviceEntryColl); |
|
| 172 |
} |
|
| 173 |
|
|
| 174 |
/**
|
|
| 175 |
* Load service schema.
|
|
| 176 |
*
|
|
| 177 |
* @param serviceSchemaColl
|
|
| 178 |
* @throws ServiceConfigurationException
|
|
| 179 |
*/
|
|
| 180 | 1 |
private void loadService(Collection serviceSchemaColl) |
| 181 |
throws ServiceConfigurationException {
|
|
| 182 | 1 |
InputStream is = null;
|
| 183 | 1 |
InputStreamReader isr = null;
|
| 184 | 1 |
Iterator iter = serviceSchemaColl.iterator(); |
| 185 | 1 |
Service service; |
| 186 | 1 |
try {
|
| 187 | 1 |
while (iter.hasNext()) {
|
| 188 | 4 |
is = ResourceHelper.getResourceAsStream((String) iter.next()); |
| 189 | 4 |
isr = new InputStreamReader(is);
|
| 190 | 4 |
service = Service.unmarshal(isr); |
| 191 | 4 |
processService(service); |
| 192 |
} |
|
| 193 |
} catch (Exception e) {
|
|
| 194 | 0 |
throw new ServiceConfigurationException(e); |
| 195 |
} finally {
|
|
| 196 | 1 |
if (is != null) { |
| 197 | 1 |
try {
|
| 198 | 1 |
is.close(); |
| 199 |
} catch (IOException e1) {
|
|
| 200 | 0 |
e1.printStackTrace(); |
| 201 |
} |
|
| 202 |
|
|
| 203 |
} |
|
| 204 |
|
|
| 205 | 1 |
if (isr != null) { |
| 206 | 1 |
try {
|
| 207 | 1 |
isr.close(); |
| 208 |
} catch (IOException e1) {
|
|
| 209 | 0 |
e1.printStackTrace(); |
| 210 |
} |
|
| 211 |
} |
|
| 212 |
|
|
| 213 |
} |
|
| 214 |
|
|
| 215 |
} |
|
| 216 |
|
|
| 217 |
/**
|
|
| 218 |
* Add service to service entry collection.
|
|
| 219 |
*
|
|
| 220 |
* @param service
|
|
| 221 |
*/
|
|
| 222 | 4 |
private void processService(Service service) { |
| 223 | 4 |
ServiceEntry[] serviceEntry = service.getServiceEntry(); |
| 224 | 4 |
for (int i = 0; i < serviceEntry.length; i++) { |
| 225 | 6 |
serviceEntryColl.add(serviceEntry[i]); |
| 226 |
} |
|
| 227 |
} |
|
| 228 |
|
|
| 229 |
/**
|
|
| 230 |
* Is service loaded?
|
|
| 231 |
*
|
|
| 232 |
* @return boolean
|
|
| 233 |
*/
|
|
| 234 | 5 |
public boolean isServiceLoaded() { |
| 235 | 5 |
return this.serviceLoaded; |
| 236 |
} |
|
| 237 |
|
|
| 238 |
/**
|
|
| 239 |
* Add service schema and interceptor to collection.
|
|
| 240 |
*
|
|
| 241 |
* @param config
|
|
| 242 |
*/
|
|
| 243 | 1 |
private void processConfig(Configuration config) { |
| 244 | 1 |
ConfigurationEntry[] configEntry = config.getServiceConfiguration() |
| 245 |
.getConfigurationEntry(); |
|
| 246 | 1 |
Interceptor[] interceptor = config.getSystemInterceptor() |
| 247 |
.getInterceptor(); |
|
| 248 | 1 |
for (int i = 0; i < configEntry.length; i++) { |
| 249 | 4 |
logger.info("Load service schema : " + configEntry[i].getValue());
|
| 250 | 4 |
serviceSchemaColl.add(configEntry[i].getValue()); |
| 251 |
} |
|
| 252 |
|
|
| 253 | 1 |
for (int i = 0; i < interceptor.length; i++) { |
| 254 | 2 |
interceptorColl.add(interceptor[i].getValue()); |
| 255 |
} |
|
| 256 |
} |
|
| 257 |
} |
|
||||||||||