Elffuss BraidHub

kikocis/java-driver public

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