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 @ 1602

History | View | Annotate | Download (5.83 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 as parameter
91
         * @param className
92
         * @return
93
         */
94
        public PersistenceFactory get(String className) {
95
                PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
96
                if (factory != null) {
97
                        return factory;
98
                } 
99
                Iterator factoriesIterator = super.iterator();
100
                while( factoriesIterator.hasNext() ) {
101
                        factory = (PersistenceFactory) factoriesIterator.next();
102
                        if( factory.getDefinition(className)!=null ) {
103
                                classToFactoryCache.put(className, factory);
104
                                return factory;
105
                        }
106
                }
107
                return null;
108
        }
109
        
110
        public PersistenceFactory get(Class theClass) {
111
                String className = theClass.getName();
112
                PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
113
                if (factory != null) {
114
            LOG.debug("DefaultFactories.get2("+theClass.getName()+") --> "+factory.getClass().getName()+" (cached).");
115
                        return factory;
116
                }
117
        List<PersistenceFactory> factories = new ArrayList<>();
118
                Iterator factoriesIterator = super.iterator();
119
                while( factoriesIterator.hasNext() ) {
120
                        factory = (PersistenceFactory) factoriesIterator.next();
121
                        if( factory.getDefinition(className)!=null || factory.manages(theClass)) {
122
                factories.add(factory);
123
                        }
124
                }
125
        if( factories.isEmpty() ) {
126
            return null;
127
        }
128
        if( factories.size()==1 ) {
129
            factory = factories.get(0);
130
            classToFactoryCache.put(className, factory);
131
            LOG.debug("DefaultFactories.get2("+theClass.getName()+") --> "+factory.getClass().getName()+".");
132
            return factory;
133
        }
134
        for( PersistenceFactory factory1 : factories ) {
135
            if( factory1.getManagedClasses().contains(theClass) ) {
136
                classToFactoryCache.put(className, factory1);
137
                LOG.debug("DefaultFactories.get2("+theClass.getName()+") ==> "+factory1.getClass().getName()+".");
138
                return factory1;
139
            }
140
        }
141
                return null;
142
        }
143
    
144
//        public PersistenceFactory get(Class theClass) {
145
//                String className = theClass.getName();
146
//                PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className);
147
//                if (factory != null) {
148
//            LOG.debug("DefaultFactories.get1("+theClass.getName()+") --> "+factory.getClass().getName()+" (cached).");
149
//                        return factory;
150
//                } 
151
//                Iterator factoriesIterator = super.iterator();
152
//                while( factoriesIterator.hasNext() ) {
153
//                        factory = (PersistenceFactory) factoriesIterator.next();
154
//                        if( factory.manages(theClass)) {
155
//                                classToFactoryCache.put(className, factory);
156
//                LOG.debug("DefaultFactories.get1("+theClass.getName()+") --> "+factory.getClass().getName()+".");
157
//                                return factory;
158
//                        }
159
//                }
160
//                return null;
161
//        }
162
    
163
        public PersistenceFactory get(Object object) {
164
                if( object instanceof String) {
165
                        return this.get((String)object);
166
                }
167
                return this.get(object.getClass());
168
        }
169

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

    
174
}