gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / locator / AbstractLocator.java @ 838
History | View | Annotate | Download (6.05 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 |
if( instance == null ) { |
65 |
logger.info("Can't locate class of '" + name + "', may be it is not registered in this locator '"+this.getLocatorName() +"'."); |
66 |
throw new ReferenceNotRegisteredException(name, this); |
67 |
} |
68 |
instances.put(name, instance); |
69 |
if( logger.isInfoEnabled() ) {
|
70 |
logger.info("Created and stored the instance of " + name + " in the singleton table (" + instance.getClass().getName() + "/" + instance.toString()+ ")."); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
return instance;
|
76 |
} |
77 |
|
78 |
public boolean exists(String name) { |
79 |
Extension extension = getExtensionPoint().get(name); |
80 |
return extension != null; |
81 |
} |
82 |
|
83 |
private void removeFromInstances(String name) { |
84 |
synchronized (lock) {
|
85 |
Object instance = instances.get(name);
|
86 |
if( instance!=null ) { |
87 |
logger.warn("Removing the instance of " + name + " from the singleton table.(" + instance.getClass().getName() + "/" + instance.toString()+ ") "); |
88 |
instances.remove(name); |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
public String[] getNames() { |
94 |
ExtensionPoint extensionPoint = getExtensionPoint(); |
95 |
List names = extensionPoint.getNames();
|
96 |
return names == null || names.size() == 0 ? null |
97 |
: (String[]) names |
98 |
.toArray(new String[names.size()]); |
99 |
} |
100 |
|
101 |
public void register(String name, Class clazz) { |
102 |
DefaultExtensionPointManager.getManager().add(getLocatorName()) |
103 |
.append(name, null, clazz);
|
104 |
logger.info("Registered class " + clazz.getName() + " in locator " + name + "."); |
105 |
removeFromInstances(name); |
106 |
} |
107 |
|
108 |
public void registerDefault(String name, Class clazz) { |
109 |
ExtensionPoint ep = getExtensionPoint(); |
110 |
if (ep.get(name) == null) { |
111 |
register(name, clazz); |
112 |
} |
113 |
} |
114 |
|
115 |
public void register(String name, String description, Class clazz) { |
116 |
DefaultExtensionPointManager.getManager().add(getLocatorName()) |
117 |
.append(name, |
118 |
description, clazz); |
119 |
logger.info("Registered class " + clazz.getName() + " in locator " + name + "."); |
120 |
removeFromInstances(name); |
121 |
} |
122 |
|
123 |
public void registerDefault(String name, String description, Class clazz) { |
124 |
ExtensionPoint ep = getExtensionPoint(); |
125 |
if( ep.get(name) == null ) { |
126 |
register(name,description, clazz); |
127 |
removeFromInstances(name); |
128 |
} |
129 |
} |
130 |
|
131 |
public void register(String name, LocatorObjectFactory factory) { |
132 |
DefaultExtensionPointManager.getManager().add(getLocatorName()).append( |
133 |
name, null,
|
134 |
factory); |
135 |
logger.info("Registered factory " + factory.getClass().getName() + "/" + factory.toString() + " in locator " + name + "."); |
136 |
removeFromInstances(name); |
137 |
} |
138 |
|
139 |
public void register(String name, String description, |
140 |
LocatorObjectFactory factory) { |
141 |
DefaultExtensionPointManager.getManager().add(getLocatorName()).append( |
142 |
name, |
143 |
description, factory); |
144 |
logger.info("Registered factory " + factory.getClass().getName() + "/" + factory.toString() + " in locator " + name + "."); |
145 |
removeFromInstances(name); |
146 |
} |
147 |
|
148 |
public String toString() { |
149 |
return getLocatorName();
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Returns the ExtensionPoint to use for the Locator values.
|
154 |
*/
|
155 |
private ExtensionPoint getExtensionPoint() {
|
156 |
ExtensionPointManager manager = DefaultExtensionPointManager |
157 |
.getManager(); |
158 |
String moduleName = getLocatorName();
|
159 |
// synchronize the retrieval of the ExtensionPoint
|
160 |
synchronized (lock) {
|
161 |
ExtensionPoint extensionPoint = manager.get(moduleName); |
162 |
if (extensionPoint == null) { |
163 |
extensionPoint = manager.add(moduleName); |
164 |
} |
165 |
return extensionPoint;
|
166 |
} |
167 |
} |
168 |
} |