Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / persistence / impl / DefaultFactories.java @ 2021

History | View | Annotate | Download (6.31 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
package org.gvsig.tools.persistence.impl;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.regex.Pattern;
33

    
34
import org.gvsig.tools.persistence.PersistenceFactory;
35
import org.gvsig.tools.persistence.PersistentState;
36
import org.gvsig.tools.persistence.PersistenceManager.Factories;
37
import org.gvsig.tools.persistence.impl.exception.PersistenceInvalidDomainNameException;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
public class DefaultFactories extends ArrayList implements Factories {
42

    
43
    private static final Logger LOG = LoggerFactory.getLogger(DefaultFactories.class);
44
    /**
45
     *
46
     */
47
    private static final long serialVersionUID = -3790704153805598236L;
48

    
49
    private static final Pattern VALID_DOMAIN_NAME_PATTERN = Pattern.compile("[\\w][\\d\\w_]*");
50

    
51
    private Map classToFactoryCache;
52

    
53
    public DefaultFactories() {
54
        this.classToFactoryCache = new HashMap();
55
        this.add(new DynObjectPersistenceFactory());
56
    }
57

    
58
    public boolean add(PersistenceFactory factory, String classNameAlias) {
59
        if (!super.contains(factory)) {
60
            this.add(factory);
61
        }
62
        this.classToFactoryCache.put(classNameAlias, factory);
63
        return true;
64
    }
65

    
66
    public boolean add(PersistenceFactory factory) {
67
        if (factory.getDomainName() == null) {
68
            throw new PersistenceInvalidDomainNameException();
69
        }
70

    
71
        if (!VALID_DOMAIN_NAME_PATTERN.matcher(factory.getDomainName()).matches()) {
72
            throw new PersistenceInvalidDomainNameException(factory.getDomainName());
73
        }
74

    
75
        List factoryManagedClasses = factory.getManagedClasses();
76
        Iterator it = factoryManagedClasses.iterator();
77
        while (it.hasNext()) {
78
            Class theClass = (Class) it.next();
79
            classToFactoryCache.put(theClass.getName(), factory);
80
        }
81
        if (this.size() < 1) {
82
            super.add(factory);
83
        } else {
84
            super.add(this.size() - 1, factory);
85
        }
86
        return true;
87
    }
88

    
89
    /**
90
     * Return the associated persistence factory to the java class name passed
91
     * as parameter
92
     *
93
     * @param className
94
     * @return
95
     */
96
    public PersistenceFactory get(String className) {
97
        PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
98
        if (factory != null) {
99
            return factory;
100
        }
101
        Iterator factoriesIterator = super.iterator();
102
        while (factoriesIterator.hasNext()) {
103
            factory = (PersistenceFactory) factoriesIterator.next();
104
            if (factory.getDefinition(className) != null) {
105
                classToFactoryCache.put(className, factory);
106
                return factory;
107
            }
108
        }
109
        return null;
110
    }
111

    
112
    public PersistenceFactory get(Class theClass) {
113
        String className = theClass.getName();
114
        PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
115
        if (factory != null) {
116
            LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") --> " + factory.getClass().getName() + " (cached).");
117
            return factory;
118
        }
119
        List<PersistenceFactory> factories = new ArrayList<>();
120
        Iterator factoriesIterator = super.iterator();
121
        while (factoriesIterator.hasNext()) {
122
            factory = (PersistenceFactory) factoriesIterator.next();
123
            if (factory.getDefinition(className) != null || factory.manages(theClass)) {
124
                factories.add(factory);
125
            }
126
        }
127
        if (factories.isEmpty()) {
128
            return null;
129
        }
130
        if (factories.size() == 1) {
131
            factory = factories.get(0);
132
            classToFactoryCache.put(className, factory);
133
            LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") --> " + factory.getClass().getName() + ".");
134
            return factory;
135
        }
136
        for (PersistenceFactory factory1 : factories) {
137
            if (factory1.getManagedClasses().contains(theClass)) {
138
                classToFactoryCache.put(className, factory1);
139
                LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") ==> " + factory1.getClass().getName() + ".");
140
                return factory1;
141
            }
142
        }
143
        return null;
144
    }
145

    
146
//        public PersistenceFactory get(Class theClass) {
147
//                String className = theClass.getName();
148
//                PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
149
//                if (factory != null) {
150
//            LOG.debug("DefaultFactories.get1("+theClass.getName()+") --> "+factory.getClass().getName()+" (cached).");
151
//                        return factory;
152
//                } 
153
//                Iterator factoriesIterator = super.iterator();
154
//                while( factoriesIterator.hasNext() ) {
155
//                        factory = (PersistenceFactory) factoriesIterator.next();
156
//                        if( factory.manages(theClass)) {
157
//                                classToFactoryCache.put(className, factory);
158
//                LOG.debug("DefaultFactories.get1("+theClass.getName()+") --> "+factory.getClass().getName()+".");
159
//                                return factory;
160
//                        }
161
//                }
162
//                return null;
163
//        }
164
    public PersistenceFactory get(Object object) {
165
        if (object instanceof String) {
166
            return this.get((String) object);
167
        }
168
        return this.get(object.getClass());
169
    }
170

    
171
    public PersistenceFactory get(PersistentState state) {
172
        return get(state.getTheClassName());
173
    }
174

    
175
}