Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / locator / AbstractLocator.java @ 827

History | View | Annotate | Download (5.02 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.ExtensionPointManager;
36
import org.gvsig.tools.extensionpoint.impl.DefaultExtensionPointManager;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
/**
41
 * Locator implementation based on the use of the ExtensionPoints.
42
 *
43
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
44
 */
45
public abstract class AbstractLocator implements Locator, Locator_withExists {
46

    
47
    private Map      instances  = new HashMap();
48
    private Logger   logger     = LoggerFactory.getLogger(AbstractLocator.class);
49
    private Object   lock       = new Object();
50

    
51
    public Object get(String name) throws LocatorException {
52
        Object instance = null;
53

    
54
        // Synchronize the creation and storage of instances
55
        synchronized (lock) {
56
            instance = instances.get(name);
57
            if (instance == null) {
58
                try {
59
                    instance = getExtensionPoint().create(name);
60
                } catch (Exception ex) {
61
                    throw new LocatorReferenceException(ex, name, this);
62
                }
63
                instances.put(name, instance);
64
                            logger.info("Stored the instance of " + name + " in the singleton table.");
65
            }
66
        }
67

    
68
        return instance;
69
    }
70

    
71
    public boolean exists(String name) {
72
        return instances.containsKey(name); 
73
    }
74
    
75
    private void removeFromInstances(String name) {
76
            synchronized (lock) {
77
                    if(instances.containsKey(name)) {
78
                            logger.info("Removing the instance of " + name + " from the singleton table.");
79
                            instances.remove(name);
80
                    }
81
            }
82
    }
83

    
84
    public String[] getNames() {
85
        ExtensionPoint extensionPoint = getExtensionPoint();
86
        List names = extensionPoint.getNames();
87
        return names == null || names.size() == 0 ? null
88
                : (String[]) names
89
                .toArray(new String[names.size()]);
90
    }
91

    
92
    public void register(String name, Class clazz) {
93
            DefaultExtensionPointManager.getManager().add(getLocatorName())
94
                                .append(name, null, clazz);
95
            removeFromInstances(name);
96
    }
97

    
98
    public void registerDefault(String name, Class clazz) {
99
                ExtensionPoint ep = getExtensionPoint();
100
                if (ep.get(name) == null) {
101
                        register(name, clazz);
102
                }
103
        }
104

    
105
    public void register(String name, String description, Class clazz) {
106
            DefaultExtensionPointManager.getManager().add(getLocatorName())
107
                                .append(name,
108
                description, clazz);
109
            removeFromInstances(name);
110
    }
111

    
112
    public void registerDefault(String name, String description, Class clazz) {
113
            ExtensionPoint ep = getExtensionPoint();
114
            if( ep.get(name) == null ) {
115
                    register(name,description, clazz);
116
                    removeFromInstances(name);
117
            }
118
    }
119

    
120
    public void register(String name, LocatorObjectFactory factory) {
121
            DefaultExtensionPointManager.getManager().add(getLocatorName()).append(
122
                                name, null,
123
                factory);
124
            removeFromInstances(name);
125
    }
126

    
127
    public void register(String name, String description,
128
            LocatorObjectFactory factory) {
129
            DefaultExtensionPointManager.getManager().add(getLocatorName()).append(
130
                                name,
131
                description, factory);
132
            removeFromInstances(name);
133
    }
134

    
135
    public String toString() {
136
        return getLocatorName();
137
    }
138

    
139
    /**
140
     * Returns the ExtensionPoint to use for the Locator values.
141
     */
142
    private ExtensionPoint getExtensionPoint() {
143
        ExtensionPointManager manager = DefaultExtensionPointManager
144
                                .getManager();
145
        String moduleName = getLocatorName();
146
        // synchronize the retrieval of the ExtensionPoint
147
        synchronized (lock) {
148
            ExtensionPoint extensionPoint = manager.get(moduleName);
149
            if (extensionPoint == null) {
150
                extensionPoint = manager.add(moduleName);
151
            }
152
            return extensionPoint;
153
        }
154
    }
155
}