Elffuss BraidHub

kikocis/java-driver public

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