Elffuss BraidHub

kikocis/java-driver public

56ea1f21527e · 7.9 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
27/**
28 * Mapping manager from which to obtain entity mappers.
29 */
30public class MappingManager {
31
32 private final Session session;
33 final boolean isCassandraV1;
34
35 private volatile Map<Class<?>, Mapper<?>> mappers = Collections.<Class<?>, Mapper<?>>emptyMap();
36 private volatile Map<Class<?>, UDTMapper<?>> udtMappers = Collections.<Class<?>, UDTMapper<?>>emptyMap();
37 private volatile Map<Class<?>, Object> accessors = Collections.<Class<?>, Object>emptyMap();
38
39 /**
40 * Creates a new {@code MappingManager} using the provided {@code Session}.
41 * <p>
42 * Note that this constructor forces the initialization of the session (see
43 * {@link #MappingManager(Session, ProtocolVersion)} if that is a problem for you).
44 *
45 * @param session the {@code Session} to use.
46 */
47 public MappingManager(Session session) {
48 this(session, getProtocolVersion(session));
49 }
50
51 private static ProtocolVersion getProtocolVersion(Session session) {
52 session.init();
53 return session.getCluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum();
54 }
55
56 /**
57 * Creates a new {@code MappingManager} using the provided {@code Session}.
58 * <p>
59 * This constructor is only provided for backward compatibility: before 2.1.7, {@code MappingManager} could be
60 * built from an uninitialized session; since 2.1.7, the mapper needs to know the active protocol version to
61 * adapt its internal requests, so {@link #MappingManager(Session)} will now initialize the session if needed.
62 * If you rely on the session not being initialized, use this constructor and provide the version manually.
63 *
64 * @param session the {@code Session} to use.
65 * @param protocolVersion the protocol version that will be used with this session.
66 *
67 * @since 2.1.7
68 */
69 public MappingManager(Session session, ProtocolVersion protocolVersion) {
70 this.session = session;
71 // This is not strictly correct because we could connect to C* 2.0 with the v1 protocol.
72 // But mappers need to make a decision early so that generated queries are compatible, and we don't know in advance
73 // which nodes might join the cluster later.
74 // At least if protocol >=2 we know there won't be any 1.2 nodes ever.
75 this.isCassandraV1 = (protocolVersion == ProtocolVersion.V1);
76 }
77
78 /**
79 * The underlying {@code Session} used by this manager.
80 * <p>
81 * Note that you can get obtain the {@code Cluster} object corresponding
82 * to that session using {@code getSession().getCluster()}.
83 * <p>
84 * It is inadvisable to close the returned Session while this manager and
85 * its mappers are in use.
86 *
87 * @return the underlying session used by this manager.
88 */
89 public Session getSession() {
90 return session;
91 }
92
93 /**
94 * Creates a {@code Mapper} for the provided class (that must be annotated by a
95 * {@link Table} annotation).
96 * <p>
97 * The {@code MappingManager} only ever keeps one Mapper for each class, and so calling this
98 * method multiple times on the same class will always return the same object.
99 *
100 * @param <T> the type of the class to map.
101 * @param klass the (annotated) class for which to return the mapper.
102 * @return the {@code Mapper} object for class {@code klass}.
103 */
104 public <T> Mapper<T> mapper(Class<T> klass) {
105 return getMapper(klass);
106 }
107
108 /**
109 * Creates a {@code UDTMapper} for the provided class (that must be
110 * annotated by a {@link com.datastax.driver.mapping.annotations.UDT} annotation).
111 *
112 * <p>
113 * The {@code MappingManager} only ever keeps one {@code UDTMapper} for each
114 * class, and so calling this method multiple times on the same class will
115 * always return the same object.
116 * </p>
117 *
118 * @param <T> the type of the class to map.
119 * @param klass the (annotated) class for which to return the mapper.
120 * @return the {@code UDTMapper} object for class {@code klass}.
121 */
122 public <T> UDTMapper<T> udtMapper(Class<T> klass) {
123 return getUDTMapper(klass);
124 }
125
126 /**
127 * Creates an accessor object based on the provided interface (that must be annotated by
128 * a {@link Accessor} annotation).
129 * <p>
130 * The {@code MappingManager} only ever keep one Accessor for each class, and so calling this
131 * method multiple time on the same class will always return the same object.
132 *
133 * @param <T> the type of the accessor class.
134 * @param klass the (annotated) class for which to create an accessor object.
135 * @return the accessor object for class {@code klass}.
136 */
137 public <T> T createAccessor(Class<T> klass) {
138 return getAccessor(klass);
139 }
140
141 @SuppressWarnings("unchecked")
142 private <T> Mapper<T> getMapper(Class<T> klass) {
143 Mapper<T> mapper = (Mapper<T>)mappers.get(klass);
144 if (mapper == null) {
145 synchronized (mappers) {
146 mapper = (Mapper<T>)mappers.get(klass);
147 if (mapper == null) {
148 EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
149 mapper = new Mapper<T>(this, klass, entityMapper);
150 Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>(mappers);
151 newMappers.put(klass, mapper);
152 mappers = newMappers;
153 }
154 }
155 }
156 return mapper;
157 }
158
159 @SuppressWarnings("unchecked")
160 <T> UDTMapper<T> getUDTMapper(Class<T> klass) {
161 UDTMapper<T> mapper = (UDTMapper<T>)udtMappers.get(klass);
162 if (mapper == null) {
163 synchronized (udtMappers) {
164 mapper = (UDTMapper<T>)udtMappers.get(klass);
165 if (mapper == null) {
166 EntityMapper<T> entityMapper = AnnotationParser.parseUDT(klass, ReflectionMapper.factory(), this);
167 mapper = new UDTMapper<T>(entityMapper, session);
168 Map<Class<?>, UDTMapper<?>> newMappers = new HashMap<Class<?>, UDTMapper<?>>(udtMappers);
169 newMappers.put(klass, mapper);
170 udtMappers = newMappers;
171 }
172 }
173 }
174 return mapper;
175 }
176
177 @SuppressWarnings("unchecked")
178 private <T> T getAccessor(Class<T> klass) {
179 T accessor = (T)accessors.get(klass);
180 if (accessor == null) {
181 synchronized (accessors) {
182 accessor = (T)accessors.get(klass);
183 if (accessor == null) {
184 AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
185 mapper.prepare(this);
186 accessor = mapper.createProxy();
187 Map<Class<?>, Object> newAccessors = new HashMap<Class<?>, Object>(accessors);
188 newAccessors.put(klass, accessor);
189 accessors = newAccessors;
190 }
191 }
192 }
193 return accessor;
194 }
195}