Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / library / impl / BaseLibrary.java @ 30580

History | View | Annotate | Download (2.78 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (CIT)
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}   {Create a base Locator implementation}
26
 */
27
package org.gvsig.tools.library.impl;
28

    
29
import java.util.HashSet;
30
import java.util.Set;
31

    
32
import org.gvsig.tools.library.Library;
33
import org.gvsig.tools.library.LibraryException;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
/**
38
 * Base Library implementation, checking that a Library is initialized and
39
 * postInitialized only once.
40
 * 
41
 * Also adds initialization logging.
42
 * 
43
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
44
 * @author jjdelcerro
45
 */
46
public abstract class BaseLibrary implements Library {
47

    
48
        public static Set initialized = new HashSet();
49
        public static Set postInitialized = new HashSet();
50

    
51
        public synchronized final void initialize() throws LibraryException {
52
                Logger logger = null;
53

    
54
                // Check if we have been already initialized
55
                String name = getName();
56
                if (initialized.contains(name)) {
57
                        return;
58
                }
59

    
60
                logger = LoggerFactory.getLogger(this.getClass());
61
                logger
62
                                .info("Initializing library '" + this.getClass().getName()
63
                                                + "'.");
64
                doInitialize();
65

    
66
                // Set the current Library as initialized
67
                initialized.add(name);
68
        }
69

    
70
        public synchronized final void postInitialize() throws LibraryException {
71
                Logger logger = null;
72

    
73
                // Check if we have been already postInitialized
74
                String name = getName();
75
                if (postInitialized.contains(name)) {
76
                        return;
77
                }
78

    
79
                logger = LoggerFactory.getLogger(this.getClass());
80
                logger.info("PostInitializing library '" + this.getClass().getName()
81
                                + "'.");
82
                doPostInitialize();
83

    
84
                // Set the current Library as postInitialized
85
                postInitialized.add(name);
86
        }
87

    
88
        protected abstract void doInitialize() throws LibraryException;
89

    
90
        protected abstract void doPostInitialize() throws LibraryException;
91

    
92
        /**
93
         * Returns the name of the current Library
94
         */
95
        private String getName() {
96
                return getClass().getName();
97
        }
98
}