|
Page Created :
OPENEJBx30 :
Singleton Example
Singleton Example has been created by David Blevins (Aug 07, 2008). Content:/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.superbiz.registry; import static javax.ejb.LockType.READ; import javax.ejb.Lock; import javax.ejb.Singleton; import java.util.HashMap; import java.util.Map; @Singleton public class ComponentRegistryBean implements ComponentRegistry { private final Map<Class, Object> components = new HashMap<Class, Object>(); @Lock(READ) public <T> T getComponent(Class<T> type) { return (T) components.get(type); } public <T> T setComponent(Class<T> type, T value) { return (T) components.put(type, value); } public <T> T removeComponent(Class<T> type) { return (T) components.remove(type); } } /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.superbiz.registry; import junit.framework.TestCase; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; import java.util.Date; import java.net.URI; public class ComponentRegistryBeanTest extends TestCase { public void test() throws Exception { Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); InitialContext context = new InitialContext(props); ComponentRegistry refOne = (ComponentRegistry) context.lookup("ComponentRegistryBeanLocal"); ComponentRegistry refTwo = (ComponentRegistry) context.lookup("ComponentRegistryBeanLocal"); refOne.setComponent(URI.class, new URI("foo://bar/baz")); URI uri = refTwo.getComponent(URI.class); assertEquals(uri, new URI("foo://bar/baz")); Date now = new Date(); refTwo.setComponent(Date.class, now); Date date = refOne.getComponent(Date.class); assertEquals(now, date); } } /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.superbiz.registry; import static javax.ejb.ConcurrencyManagementType.BEAN; import javax.ejb.Singleton; import javax.ejb.ConcurrencyManagement; import java.util.Properties; @Singleton @ConcurrencyManagement(BEAN) public class PropertyRegistryBean implements PropertyRegistry { private final Properties properties = new Properties(); public String getProperty(String key) { return properties.getProperty(key); } public String setProperty(String key, String value) { return (String) properties.setProperty(key, value); } public String removeProperty(String key) { return (String) properties.remove(key); } } /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.superbiz.registry; import junit.framework.TestCase; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; public class PropertiesRegistryBeanTest extends TestCase { public void test() throws Exception { Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); InitialContext context = new InitialContext(props); PropertyRegistry refOne = (PropertyRegistry) context.lookup("PropertyRegistryBeanLocal"); PropertyRegistry refTwo = (PropertyRegistry) context.lookup("PropertyRegistryBeanLocal"); refOne.setProperty("url", "http://superbiz.org"); String url = "" class="code-quote">"url"); assertEquals("http://superbiz.org", url); refTwo.setProperty("version", "1.0.5"); String version = refOne.getProperty("version"); assertEquals("1.0.5", version); } } |
Powered by
Atlassian Confluence
(Version: 2.2.9 Build:#527 Sep 07, 2006)
-
Bug/feature request
Unsubscribe or edit your notifications preferences
Unsubscribe or edit your notifications preferences
