View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.limmen.crs.client.locator;
17  
18  import static org.limmen.crs.Constants.SERVICE_CONTEXTPATH_PROPERTY;
19  import static org.limmen.crs.Constants.SERVICE_HOSTNAME_PROPERTY;
20  import static org.limmen.crs.Constants.SERVICE_PORT_PROPERTY;
21  
22  public class StaticServiceLocator implements ServiceLocator {
23  
24  	@Override
25  	public Service locate() throws ServiceLocatorException {
26  
27  		Service service = null;
28  
29  		if (System.getProperty(SERVICE_HOSTNAME_PROPERTY) != null
30  				&& System.getProperty(SERVICE_PORT_PROPERTY) != null) {
31  
32  			String hostname = System.getProperty(SERVICE_HOSTNAME_PROPERTY);
33  			int port = Integer.parseInt(System.getProperty(SERVICE_PORT_PROPERTY));
34  			String contextPath = System.getProperty(SERVICE_CONTEXTPATH_PROPERTY);
35  
36  			service = new Service(hostname, port, contextPath);
37  		}
38  		else {
39  
40  			if (System.getProperty(SERVICE_HOSTNAME_PROPERTY) == null) {
41  
42  				throw new ServiceLocatorException(String.format("Property '%0$s' not found!", SERVICE_HOSTNAME_PROPERTY));
43  			}
44  			if (System.getProperty(SERVICE_PORT_PROPERTY) == null) {
45  
46  				throw new ServiceLocatorException(String.format("Property '%0$s' not found!", SERVICE_PORT_PROPERTY));
47  			}
48  		}
49  
50  		return service;
51  	}
52  }