gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / locator / AbstractLocator.java @ 832
History | View | Annotate | Download (5.78 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2008 {DiSiD Technologies} {Create a base Locator implementation}
|
27 |
*/
|
28 |
package org.gvsig.tools.locator; |
29 |
|
30 |
import java.util.HashMap; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
|
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
35 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
36 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
37 |
import org.gvsig.tools.extensionpoint.impl.DefaultExtensionPointManager; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
/**
|
42 |
* Locator implementation based on the use of the ExtensionPoints.
|
43 |
*
|
44 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
45 |
*/
|
46 |
public abstract class AbstractLocator implements Locator, Locator_withExists { |
47 |
|
48 |
private Map instances = new HashMap(); |
49 |
private Logger logger = LoggerFactory.getLogger(AbstractLocator.class); |
50 |
private Object lock = new Object(); |
51 |
|
52 |
public Object get(String name) throws LocatorException { |
53 |
Object instance = null; |
54 |
|
55 |
// Synchronize the creation and storage of instances
|
56 |
synchronized (lock) {
|
57 |
instance = instances.get(name); |
58 |
if (instance == null) { |
59 |
try {
|
60 |
instance = getExtensionPoint().create(name); |
61 |
} catch (Exception ex) { |
62 |
throw new LocatorReferenceException(ex, name, this); |
63 |
} |
64 |
instances.put(name, instance); |
65 |
if( logger.isInfoEnabled() ) {
|
66 |
logger.info("Created and stored the instance of " + name + " in the singleton table (" + instance.getClass().getName() + "/" + instance.toString()+ ")."); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
return instance;
|
72 |
} |
73 |
|
74 |
public boolean exists(String name) { |
75 |
Extension extension = getExtensionPoint().get(name); |
76 |
return extension != null; |
77 |
} |
78 |
|
79 |
private void removeFromInstances(String name) { |
80 |
synchronized (lock) {
|
81 |
Object instance = instances.get(name);
|
82 |
if( instance!=null ) { |
83 |
logger.warn("Removing the instance of " + name + " from the singleton table.(" + instance.getClass().getName() + "/" + instance.toString()+ ") "); |
84 |
instances.remove(name); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
public String[] getNames() { |
90 |
ExtensionPoint extensionPoint = getExtensionPoint(); |
91 |
List names = extensionPoint.getNames();
|
92 |
return names == null || names.size() == 0 ? null |
93 |
: (String[]) names |
94 |
.toArray(new String[names.size()]); |
95 |
} |
96 |
|
97 |
public void register(String name, Class clazz) { |
98 |
DefaultExtensionPointManager.getManager().add(getLocatorName()) |
99 |
.append(name, null, clazz);
|
100 |
logger.info("Registered class " + clazz.getName() + " in locator " + name + "."); |
101 |
removeFromInstances(name); |
102 |
} |
103 |
|
104 |
public void registerDefault(String name, Class clazz) { |
105 |
ExtensionPoint ep = getExtensionPoint(); |
106 |
if (ep.get(name) == null) { |
107 |
register(name, clazz); |
108 |
} |
109 |
} |
110 |
|
111 |
public void register(String name, String description, Class clazz) { |
112 |
DefaultExtensionPointManager.getManager().add(getLocatorName()) |
113 |
.append(name, |
114 |
description, clazz); |
115 |
logger.info("Registered class " + clazz.getName() + " in locator " + name + "."); |
116 |
removeFromInstances(name); |
117 |
} |
118 |
|
119 |
public void registerDefault(String name, String description, Class clazz) { |
120 |
ExtensionPoint ep = getExtensionPoint(); |
121 |
if( ep.get(name) == null ) { |
122 |
register(name,description, clazz); |
123 |
removeFromInstances(name); |
124 |
} |
125 |
} |
126 |
|
127 |
public void register(String name, LocatorObjectFactory factory) { |
128 |
DefaultExtensionPointManager.getManager().add(getLocatorName()).append( |
129 |
name, null,
|
130 |
factory); |
131 |
logger.info("Registered factory " + factory.getClass().getName() + "/" + factory.toString() + " in locator " + name + "."); |
132 |
removeFromInstances(name); |
133 |
} |
134 |
|
135 |
public void register(String name, String description, |
136 |
LocatorObjectFactory factory) { |
137 |
DefaultExtensionPointManager.getManager().add(getLocatorName()).append( |
138 |
name, |
139 |
description, factory); |
140 |
logger.info("Registered factory " + factory.getClass().getName() + "/" + factory.toString() + " in locator " + name + "."); |
141 |
removeFromInstances(name); |
142 |
} |
143 |
|
144 |
public String toString() { |
145 |
return getLocatorName();
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Returns the ExtensionPoint to use for the Locator values.
|
150 |
*/
|
151 |
private ExtensionPoint getExtensionPoint() {
|
152 |
ExtensionPointManager manager = DefaultExtensionPointManager |
153 |
.getManager(); |
154 |
String moduleName = getLocatorName();
|
155 |
// synchronize the retrieval of the ExtensionPoint
|
156 |
synchronized (lock) {
|
157 |
ExtensionPoint extensionPoint = manager.get(moduleName); |
158 |
if (extensionPoint == null) { |
159 |
extensionPoint = manager.add(moduleName); |
160 |
} |
161 |
return extensionPoint;
|
162 |
} |
163 |
} |
164 |
} |