001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import java.util.Properties;
020: import java.util.HashMap;
021:
022: public class ClientInstance {
023: /**
024: * The time the client instance class was initialized
025: */
026: private final long startTime = System.currentTimeMillis();
027:
028: /**
029: * Properties that have to be away from System (i.e. {@link System#setProperty(String, String)} must not be called)
030: */
031: private final Properties internalProperties = new Properties();
032:
033: /**
034: * Global component registry
035: */
036: private final HashMap<Class, Object> components;
037:
038: private ClientInstance(Properties properties) throws Exception {
039: this .components = new HashMap<Class, Object>();
040:
041: this .internalProperties.putAll(System.getProperties());
042: this .internalProperties.putAll(properties);
043: }
044:
045: public long getStartTime() {
046: return startTime;
047: }
048:
049: public Properties getProperties() {
050: return internalProperties;
051: }
052:
053: public String getProperty(String key) {
054: return internalProperties.getProperty(key);
055: }
056:
057: public String getProperty(String key, String defaultValue) {
058: return internalProperties.getProperty(key, defaultValue);
059: }
060:
061: public Object setProperty(String key, String value) {
062: return internalProperties.setProperty(key, value);
063: }
064:
065: /**
066: * @param propName property name
067: *
068: * @return true when property is set; false otherwise
069: */
070: public boolean hasProperty(String propName) {
071: return this .internalProperties.get(propName) != null;
072: }
073:
074: /**
075: * Gets a global component instance.
076: *
077: * @param type the class type of the component - required
078: * @return the object associated with the class type or null
079: * @throws IllegalStateException of the component isn't found
080: */
081: @SuppressWarnings({"unchecked"})
082: public <T> T getComponent(Class<T> type) {
083: return (T) components.get(type);
084: }
085:
086: /**
087: * Removes a global component instance.
088: *
089: * @param type the class type of the component - required
090: * @return the component instance or null if component type was not registered
091: */
092: @SuppressWarnings({"unchecked"})
093: public <T> T removeComponent(Class<T> type) {
094: return (T) components.remove(type);
095: }
096:
097: /**
098: * Registers a component instance with the client, so it may be reference globally.
099: *
100: * @param type the class type of the component - required
101: * @param component the component instance
102: */
103: @SuppressWarnings({"unchecked"})
104: public <T> T setComponent(Class<T> type, T component) {
105: return (T) components.put(type, component);
106: }
107:
108: private static ClientInstance client;
109:
110: static {
111: try {
112: client = new ClientInstance(System.getProperties());
113: } catch (Exception e) {
114: throw new RuntimeException(
115: "Failed to create default instance of SystemInstance",
116: e);
117: }
118: }
119:
120: public static ClientInstance get() {
121: return client;
122: }
123: }
|