Elffuss BraidHub

kikocis/java-driver public

96c0afc7f127 · 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 com.datastax.driver.core.ProtocolVersion;
19import com.datastax.driver.core.Session;
20import com.datastax.driver.mapping.annotations.Accessor;
21import com.datastax.driver.mapping.annotations.Table;
22
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.Map;
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 * @since 2.1.7
67 */
68 public MappingManager(Session session, ProtocolVersion protocolVersion) {
69 this.session = session;
70 // This is not strictly correct because we could connect to C* 2.0 with the v1 protocol.
71 // But mappers need to make a decision early so that generated queries are compatible, and we don't know in advance
72 // which nodes might join the cluster later.
73 // At least if protocol >=2 we know there won't be any 1.2 nodes ever.
74 this.isCassandraV1 = (protocolVersion == ProtocolVersion.V1);
75 }
76
77 /**
78 * The underlying {@code Session} used by this manager.
79 * <p/>
80 * Note that you can get obtain the {@code Cluster} object corresponding
81 * to that session using {@code getSession().getCluster()}.
82 * <p/>
83 * It is inadvisable to close the returned Session while this manager and
84 * its mappers are in use.
85 *
86 * @return the underlying session used by this manager.
87 */
88 public Session getSession() {
89 return session;
90 }
91
92 /**
93 * Creates a {@code Mapper} for the provided class (that must be annotated by a
94 * {@link Table} annotation).
95 * <p/>
96 * The {@code MappingManager} only ever keeps one Mapper for each class, and so calling this
97 * method multiple times on the same class will always return the same object.
98 *
99 * @param <T> the type of the class to map.
100 * @param klass the (annotated) class for which to return the mapper.
101 * @return the {@code Mapper} object for class {@code klass}.
102 */
103 public <T> Mapper<T> mapper(Class<T> klass) {
104 return getMapper(klass);
105 }
106
107 /**
108 * Creates a {@code UDTMapper} for the provided class (that must be
109 * annotated by a {@link com.datastax.driver.mapping.annotations.UDT} annotation).
110 * <p/>
111 * <p>
112 * The {@code MappingManager} only ever keeps one {@code UDTMapper} for each
113 * class, and so calling this method multiple times on the same class will
114 * always return the same object.
115 * </p>
116 *
117 * @param <T> the type of the class to map.
118 * @param klass the (annotated) class for which to return the mapper.
119 * @return the {@code UDTMapper} object for class {@code klass}.
120 */
121 public <T> UDTMapper<T> udtMapper(Class<T> klass) {
122 return getUDTMapper(klass);
123 }
124
125 /**
126 * Creates an accessor object based on the provided interface (that must be annotated by
127 * a {@link Accessor} annotation).
128 * <p/>
129 * The {@code MappingManager} only ever keep one Accessor for each class, and so calling this
130 * method multiple time on the same class will always return the same object.
131 *
132 * @param <T> the type of the accessor class.
133 * @param klass the (annotated) class for which to create an accessor object.
134 * @return the accessor object for class {@code klass}.
135 */
136 public <T> T createAccessor(Class<T> klass) {
137 return getAccessor(klass);
138 }
139
140 @SuppressWarnings("unchecked")
141 private <T> Mapper<T> getMapper(Class<T> klass) {
142 Mapper<T> mapper = (Mapper<T>) mappers.get(klass);
143 if (mapper == null) {
144 synchronized (mappers) {
145 mapper = (Mapper<T>) mappers.get(klass);
146 if (mapper == null) {
147 EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
148 mapper = new Mapper<T>(this, klass, entityMapper);
149 Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>(mappers);
150 newMappers.put(klass, mapper);
151 mappers = newMappers;
152 }
153 }
154 }
155 return mapper;
156 }
157
158 @SuppressWarnings("unchecked")
159 <T> UDTMapper<T> getUDTMapper(Class<T> klass) {
160 UDTMapper<T> mapper = (UDTMapper<T>) udtMappers.get(klass);
161 if (mapper == null) {
162 synchronized (udtMappers) {
163 mapper = (UDTMapper<T>) udtMappers.get(klass);
164 if (mapper == null) {
165 EntityMapper<T> entityMapper = AnnotationParser.parseUDT(klass, ReflectionMapper.factory(), this);
166 mapper = new UDTMapper<T>(entityMapper, session);
167 Map<Class<?>, UDTMapper<?>> newMappers = new HashMap<Class<?>, UDTMapper<?>>(udtMappers);
168 newMappers.put(klass, mapper);
169 udtMappers = newMappers;
170 }
171 }
172 }
173 return mapper;
174 }
175
176 @SuppressWarnings("unchecked")
177 private <T> T getAccessor(Class<T> klass) {
178 T accessor = (T) accessors.get(klass);
179 if (accessor == null) {
180 synchronized (accessors) {
181 accessor = (T) accessors.get(klass);
182 if (accessor == null) {
183 AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
184 mapper.prepare(this);
185 accessor = mapper.createProxy();
186 Map<Class<?>, Object> newAccessors = new HashMap<Class<?>, Object>(accessors);
187 newAccessors.put(klass, accessor);
188 accessors = newAccessors;
189 }
190 }
191 }
192 return accessor;
193 }
194}