Statistics
| Revision:

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

History | View | Annotate | Download (3.52 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
package org.gvsig.tools.library.impl;
24

    
25
import java.util.Iterator;
26
import java.util.Set;
27

    
28
import org.gvsig.tools.library.LibrariesInitializer;
29
import org.gvsig.tools.library.Library;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
/**
34
 * Base implementation of an {@link LibrariesInitializer} with the
35
 * initialization of Libraries already implemented, delegating on child classes
36
 * the finding of available {@link Library} objects.
37
 * <p>
38
 * This class is NOT thread safe.
39
 * </p>
40
 * 
41
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
42
 */
43
public abstract class AbstractLibrariesInitializer implements
44
                LibrariesInitializer {
45

    
46
        private static final Logger LOG = LoggerFactory
47
                        .getLogger(AbstractLibrariesInitializer.class);
48

    
49
        private Set libs;
50

    
51
        private final ClassLoader classLoader;
52

    
53
        private boolean libsLoaded = false;
54

    
55
        public AbstractLibrariesInitializer() {
56
                classLoader = null;
57
        }
58

    
59
        public AbstractLibrariesInitializer(ClassLoader classLoader) {
60
                this.classLoader = classLoader;
61
        }
62

    
63
        public void initialize() {
64
                if (!libsLoaded) {
65
                        loadLibraries(classLoader);
66
                        libsLoaded = true;
67
                }
68
                if (libs != null && libs.size() > 0) {
69
                        // Call all initialize()
70
                        initializeLibraries(libs, false);
71
                }
72
        }
73

    
74
        public void postInitialize() {
75
                if (!libsLoaded) {
76
                        loadLibraries(classLoader);
77
                        libsLoaded = true;
78
                }
79
                if (libs != null && libs.size() > 0) {
80
                        // Call all postInitialize()
81
                        initializeLibraries(libs, true);
82
                }
83
        }
84

    
85
        public void fullInitialize() {
86
                initialize();
87
                postInitialize();
88
        }
89

    
90
        protected abstract Set findLibraries(Class libraryClass,
91
                        ClassLoader classLoader);
92

    
93
        private void loadLibraries(ClassLoader classLoader) {
94
                // Find the available Library instances
95
                libs = findLibraries(Library.class, classLoader);
96

    
97
                if (LOG.isInfoEnabled()) {
98
                        logLibraries(libs);
99
                }
100
        }
101

    
102
        private void initializeLibraries(Set libs, boolean post) {
103
                for (Iterator iterator = libs.iterator(); iterator.hasNext();) {
104
                        Library lib = (Library) iterator.next();
105
                        if (post) {
106
                                LOG.trace("Calling {}.postInitialize()", lib.getClass()
107
                                                .getName());
108
                                lib.postInitialize();
109
                        } else {
110
                                LOG.trace("Calling {}.initialize()", lib.getClass().getName());
111
                                lib.initialize();
112
                        }
113
                }
114
        }
115

    
116
        private void logLibraries(Set libs) {
117
                StringBuffer buffer = new StringBuffer();
118
                buffer.append("Found ").append(libs == null ? 0 : libs.size()).append(
119
                                " libraries:\n");
120
                if (libs != null) {
121
                        for (Iterator iterator = libs.iterator(); iterator.hasNext();) {
122
                                Library lib = (Library) iterator.next();
123
                                buffer.append("\t").append(lib.getClass().getName()).append(
124
                                                "\n");
125
                        }
126
                }
127
                LOG.info(buffer.toString());
128
        }
129

    
130
}