View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.mapping;
22  
23  import org.apache.hadoop.hbase.hbql.client.HBqlException;
24  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
25  
26  import java.io.Serializable;
27  
28  public class MappingContext implements Serializable {
29  
30      private static final long serialVersionUID = 1L;
31  
32      private String mappingName = null;
33      private Mapping mapping = null;
34      private ResultAccessor resultAccessor = null;
35  
36      public MappingContext() {
37      }
38  
39      public MappingContext(final Mapping mapping) {
40          this.setMapping(mapping);
41      }
42  
43      public MappingContext(final String mappingName) {
44          this.setMappingName(mappingName);
45      }
46  
47      public String getMappingName() {
48          return mappingName;
49      }
50  
51      private void setMappingName(final String mappingName) {
52          this.mappingName = mappingName;
53      }
54  
55      public void setMapping(final Mapping mapping) {
56          this.mapping = mapping;
57          if (this.getMapping() != null)
58              this.setMappingName(this.getMapping().getMappingName());
59      }
60  
61      public Mapping getMapping() {
62          return this.mapping;
63      }
64  
65      public ResultAccessor getResultAccessor() {
66          return this.resultAccessor;
67      }
68  
69      public void setResultAccessor(final ResultAccessor resultAccessor) {
70          this.resultAccessor = resultAccessor;
71      }
72  
73      public TableMapping getTableMapping() {
74          return (TableMapping)this.getMapping();
75      }
76  
77      public synchronized void validateMappingName(final HConnectionImpl conn) throws HBqlException {
78  
79          if (this.getMapping() == null) {
80              try {
81                  this.setMapping(conn.getMapping(this.getMappingName()));
82              }
83              catch (HBqlException e) {
84                  e.printStackTrace();
85                  throw new HBqlException("Unknown mapping name: " + this.getMappingName());
86              }
87          }
88  
89          this.validateMatchingNames(this.getResultAccessor());
90      }
91  
92      private void validateMatchingNames(final ResultAccessor accessor) throws HBqlException {
93          if (accessor != null && accessor instanceof AnnotationResultAccessor) {
94              final String mappingName = accessor.getMapping().getMappingName();
95              final String selectName = this.getMapping().getMappingName();
96              if (!mappingName.equals(selectName))
97                  throw new HBqlException("Class " + mappingName + " instead of " + selectName);
98          }
99      }
100 }