Elffuss BraidHub

kikocis/java-driver public

4c6dd1e29c05 · 6.2 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import java.util.Collections;
19import java.util.HashMap;
20import java.util.Map;
21
22import com.datastax.driver.core.*;
23
24import com.datastax.driver.mapping.annotations.Accessor;
25import com.datastax.driver.mapping.annotations.Table;
26
27import com.datastax.driver.mapping.annotations.UDT;
28
29/**
30 * Mapping manager from which to obtain entity mappers.
31 */
32public class MappingManager {
33
34 private final Session session;
35
36 private volatile Map<Class<?>, Mapper<?>> mappers = Collections.<Class<?>, Mapper<?>>emptyMap();
37 private volatile Map<Class<?>, UDTMapper<?>> udtMappers = Collections.<Class<?>, UDTMapper<?>>emptyMap();
38 private volatile Map<Class<?>, Object> accessors = Collections.<Class<?>, Object>emptyMap();
39
40 /**
41 * Creates a new {@code MappingManager} using the provided {@code Session}.
42 *
43 * @param session the {@code Session} to use.
44 */
45 public MappingManager(Session session) {
46 this.session = session;
47 }
48
49 /**
50 * The underlying {@code Session} used by this manager.
51 * <p>
52 * Note that you can get obtain the {@code Cluster} object corresponding
53 * to that session using {@code getSession().getCluster()}.
54 * <p>
55 * It is inadvisable to close the returned Session while this manager and
56 * its mappers are in use.
57 *
58 * @return the underlying session used by this manager.
59 */
60 public Session getSession() {
61 return session;
62 }
63
64 /**
65 * Creates a {@code Mapper} for the provided class (that must be annotated by a
66 * {@link Table} annotation).
67 * <p>
68 * The {@code MappingManager} only ever keeps one Mapper for each class, and so calling this
69 * method multiple times on the same class will always return the same object.
70 *
71 * @param <T> the type of the class to map.
72 * @param klass the (annotated) class for which to return the mapper.
73 * @return the {@code Mapper} object for class {@code klass}.
74 */
75 public <T> Mapper<T> mapper(Class<T> klass) {
76 return getMapper(klass);
77 }
78
79 /**
80 * Creates a {@code UDTMapper} for the provided class (that must be
81 * annotated by a {@link com.datastax.driver.mapping.annotations.UDT} annotation).
82 *
83 * <p>
84 * The {@code MappingManager} only ever keeps one {@code UDTMapper} for each
85 * class, and so calling this method multiple times on the same class will
86 * always return the same object.
87 * </p>
88 *
89 * @param <T> the type of the class to map.
90 * @param klass the (annotated) class for which to return the mapper.
91 * @return the {@code UDTMapper} object for class {@code klass}.
92 */
93 public <T> UDTMapper<T> udtMapper(Class<T> klass) {
94 return getUDTMapper(klass);
95 }
96
97 /**
98 * Creates an accessor object based on the provided interface (that must be annotated by
99 * a {@link Accessor} annotation).
100 * <p>
101 * The {@code MappingManager} only ever keep one Accessor for each class, and so calling this
102 * method multiple time on the same class will always return the same object.
103 *
104 * @param <T> the type of the accessor class.
105 * @param klass the (annotated) class for which to create an accessor object.
106 * @return the accessor object for class {@code klass}.
107 */
108 public <T> T createAccessor(Class<T> klass) {
109 return getAccessor(klass);
110 }
111
112 @SuppressWarnings("unchecked")
113 private <T> Mapper<T> getMapper(Class<T> klass) {
114 Mapper<T> mapper = (Mapper<T>)mappers.get(klass);
115 if (mapper == null) {
116 synchronized (mappers) {
117 mapper = (Mapper<T>)mappers.get(klass);
118 if (mapper == null) {
119 EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
120 mapper = new Mapper<T>(this, klass, entityMapper);
121 Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>(mappers);
122 newMappers.put(klass, mapper);
123 mappers = newMappers;
124 }
125 }
126 }
127 return mapper;
128 }
129
130 @SuppressWarnings("unchecked")
131 <T> UDTMapper<T> getUDTMapper(Class<T> klass) {
132 UDTMapper<T> mapper = (UDTMapper<T>)udtMappers.get(klass);
133 if (mapper == null) {
134 synchronized (udtMappers) {
135 mapper = (UDTMapper<T>)udtMappers.get(klass);
136 if (mapper == null) {
137 EntityMapper<T> entityMapper = AnnotationParser.parseUDT(klass, ReflectionMapper.factory(), this);
138 mapper = new UDTMapper<T>(entityMapper, session);
139 Map<Class<?>, UDTMapper<?>> newMappers = new HashMap<Class<?>, UDTMapper<?>>(udtMappers);
140 newMappers.put(klass, mapper);
141 udtMappers = newMappers;
142 }
143 }
144 }
145 return mapper;
146 }
147
148 @SuppressWarnings("unchecked")
149 private <T> T getAccessor(Class<T> klass) {
150 T accessor = (T)accessors.get(klass);
151 if (accessor == null) {
152 synchronized (accessors) {
153 accessor = (T)accessors.get(klass);
154 if (accessor == null) {
155 AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
156 mapper.prepare(this);
157 accessor = mapper.createProxy();
158 Map<Class<?>, Object> newAccessors = new HashMap<Class<?>, Object>(accessors);
159 newAccessors.put(klass, accessor);
160 accessors = newAccessors;
161 }
162 }
163 }
164 return accessor;
165 }
166}