1 /*
2 * Copyright 2004-2008 the Seasar Foundation and the Others.
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,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package org.seasar.uruma.rcp.core;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.ServiceFactory;
23 import org.osgi.framework.ServiceRegistration;
24 import org.seasar.uruma.rcp.UrumaService;
25
26 /**
27 * {@link UrumaService} のための {@link ServiceFactory} です。
28 *
29 * @author y-komori
30 */
31 public class UrumaServiceFactory implements ServiceFactory {
32 protected static final Map<String, UrumaService> serviceMap = new HashMap<String, UrumaService>();
33
34 UrumaServiceFactory() {
35
36 }
37
38 /*
39 * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle,
40 * org.osgi.framework.ServiceRegistration)
41 */
42 public Object getService(final Bundle bundle,
43 final ServiceRegistration registration) {
44 String symbolicName = bundle.getSymbolicName();
45 UrumaService service = serviceMap.get(symbolicName);
46 if (service == null) {
47 service = new UrumaServiceImpl(bundle);
48 serviceMap.put(symbolicName, service);
49 }
50 return service;
51 }
52
53 /*
54 * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle,
55 * org.osgi.framework.ServiceRegistration, java.lang.Object)
56 */
57 public void ungetService(final Bundle bundle,
58 final ServiceRegistration registration, final Object service) {
59 String symbolicName = bundle.getSymbolicName();
60 UrumaServiceImpl urumaService = (UrumaServiceImpl) serviceMap
61 .get(symbolicName);
62 if (urumaService != null) {
63 urumaService.destroy();
64 serviceMap.remove(symbolicName);
65 }
66 }
67 }