KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > ClassDescriptorMap


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.reflect;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.map.EntityResolver;
29
30 /**
31  * An object that holds class descriptors for mapped entities, compiling new descriptors
32  * on demand using an internal chain of descriptor factories.
33  *
34  * @since 3.0
35  * @author Andrus Adamchik
36  */

37 public class ClassDescriptorMap {
38
39     protected EntityResolver resolver;
40     protected Map JavaDoc descriptors;
41     protected List JavaDoc factories;
42
43     public ClassDescriptorMap(EntityResolver resolver) {
44         this.descriptors = new HashMap JavaDoc();
45         this.resolver = resolver;
46         this.factories = new ArrayList JavaDoc();
47     }
48
49     public EntityResolver getResolver() {
50         return resolver;
51     }
52
53     /**
54      * Adds a factory to the descriptor factory chain.
55      */

56     public void addFactory(ClassDescriptorFactory factory) {
57         factories.add(factory);
58     }
59
60     public void removeFactory(ClassDescriptorFactory factory) {
61         factories.remove(factory);
62     }
63
64     public void clearFactories() {
65         factories.clear();
66     }
67
68     public void clearDescriptors() {
69         descriptors.clear();
70     }
71
72     /**
73      * Removes cached descriptor if any for the given entity.
74      */

75     public void removeDescriptor(String JavaDoc entityName) {
76         descriptors.remove(entityName);
77     }
78
79     /**
80      * Caches descriptor definition.
81      */

82     public void addDescriptor(String JavaDoc entityName, ClassDescriptor descriptor) {
83         if (descriptor == null) {
84             removeDescriptor(entityName);
85         }
86         else {
87             descriptors.put(entityName, descriptor);
88         }
89     }
90
91     public ClassDescriptor getDescriptor(String JavaDoc entityName) {
92         ClassDescriptor cached = (ClassDescriptor) descriptors.get(entityName);
93         if (cached != null) {
94             return cached;
95         }
96
97         return createProxyDescriptor(entityName);
98     }
99
100     /**
101      * Creates a descriptor wrapper that will compile the underlying descriptor on demand.
102      * Using proxy indirection is needed to compile relationships of descriptors to other
103      * descriptors that are not compiled yet.
104      */

105     protected ClassDescriptor createProxyDescriptor(String JavaDoc entityName) {
106         ClassDescriptor descriptor = new LazyClassDescriptorDecorator(this, entityName);
107         addDescriptor(entityName, descriptor);
108         return descriptor;
109     }
110
111     /**
112      * Creates a new descriptor.
113      */

114     protected ClassDescriptor createDescriptor(String JavaDoc entityName) {
115
116         // scan the factory chain until some factory returns a non-null descriptor;
117
// scanning is done in reverse order so that the factories added last take higher
118
// precedence...
119
ListIterator JavaDoc it = factories.listIterator(factories.size());
120         while (it.hasPrevious()) {
121             ClassDescriptorFactory factory = (ClassDescriptorFactory) it.previous();
122             ClassDescriptor descriptor = factory.getDescriptor(entityName);
123
124             if (descriptor != null) {
125                 return descriptor;
126             }
127         }
128
129         throw new CayenneRuntimeException("Failed to create descriptor for entity: "
130                 + entityName);
131     }
132 }
133
Popular Tags