View Javadoc

1   /*
2    * Copyright (c) 2011.  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.impl;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.mapping.HRecordResultAccessor;
26  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
27  import org.apache.hadoop.hbase.hbql.parser.ParserUtil;
28  import org.apache.hadoop.hbase.hbql.statement.ConnectionStatement;
29  import org.apache.hadoop.hbase.hbql.statement.DeleteStatement;
30  import org.apache.hadoop.hbase.hbql.statement.HBqlStatement;
31  import org.apache.hadoop.hbase.hbql.statement.InsertStatement;
32  import org.apache.hadoop.hbase.hbql.statement.NonConnectionStatement;
33  import org.apache.hadoop.hbase.hbql.statement.SelectStatement;
34  import org.apache.hadoop.hbase.hbql.util.Lists;
35  
36  import java.io.ByteArrayOutputStream;
37  import java.io.PrintWriter;
38  import java.lang.reflect.Field;
39  import java.lang.reflect.Modifier;
40  import java.util.List;
41  import java.util.Random;
42  
43  
44  public class Utils {
45  
46      public static HBqlStatement parseHBqlStatement(final String sql) throws HBqlException {
47  
48          try {
49              final HBqlStatement stmt = ParserUtil.parseHBqlStatement(sql);
50  
51              if (!Utils.isSelectStatement(stmt)
52                  && !(stmt instanceof ConnectionStatement)
53                  && !(stmt instanceof NonConnectionStatement))
54                  throw new HBqlException("Unsupported statement type: " + stmt.getClass().getSimpleName() + " - " + sql);
55  
56              if (stmt instanceof MappingContext) {
57                  final MappingContext mappingContext = (MappingContext)stmt;
58                  mappingContext.setResultAccessor(new HRecordResultAccessor(mappingContext));
59              }
60  
61              return stmt;
62          }
63          catch (ParseException e) {
64              throw new HBqlException(e.getErrorMessage());
65          }
66      }
67  
68      public static boolean isSelectStatement(final HBqlStatement stmt) {
69          return stmt instanceof SelectStatement;
70      }
71  
72      public static boolean isDMLStatement(final HBqlStatement stmt) {
73          return stmt instanceof InsertStatement || stmt instanceof DeleteStatement;
74      }
75  
76      public static boolean isConnectionStatemet(final HBqlStatement stmt) {
77          return stmt instanceof ConnectionStatement;
78      }
79  
80      public static boolean isNonConectionStatemet(final HBqlStatement stmt) {
81          return stmt instanceof NonConnectionStatement;
82      }
83  
84      private static List<Class> classList = Lists.newArrayList();
85  
86      public static void checkForDefaultConstructors(final Class clazz) {
87  
88          if (!clazz.getName().contains("hadoop"))
89              return;
90  
91          if (Modifier.isStatic(clazz.getModifiers()))
92              return;
93  
94          if (classList.contains(clazz))
95              return;
96          else
97              classList.add(clazz);
98  
99          if (!hasDefaultConstructor(clazz))
100             System.out.println(clazz.getName() + " is missing null constructor");
101 
102         Field[] fields = clazz.getDeclaredFields();
103 
104         for (final Field field : fields) {
105             Class dclazz = field.getType();
106             checkForDefaultConstructors(dclazz);
107         }
108 
109         fields = clazz.getFields();
110 
111         for (final Field field : fields) {
112             Class dclazz = field.getType();
113             checkForDefaultConstructors(dclazz);
114         }
115     }
116 
117     public static boolean hasDefaultConstructor(final Class clazz) {
118         try {
119             clazz.getConstructor();
120         }
121         catch (NoSuchMethodException e) {
122             return false;
123         }
124         return true;
125     }
126 
127     public static boolean isValidString(final String val) {
128         return val != null && val.trim().length() > 0;
129     }
130 
131     private static Random randomVal = new Random();
132 
133 
134     public static boolean getRandomBoolean() {
135         return randomVal.nextBoolean();
136     }
137 
138     public static int getRandomPositiveInt(final int upper) {
139         while (true) {
140             final int val = randomVal.nextInt();
141             // Math.abs(Integer.MIN_VALUE = Integer.MIN_VALUE, which is still negative
142             // So try again if it comes up
143             if (val != Integer.MIN_VALUE)
144                 return (Math.abs(val) % upper) + 1;
145         }
146     }
147 
148     public static void logException(final Log log, final Exception e) {
149 
150         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
151         final PrintWriter oos = new PrintWriter(baos);
152 
153         e.printStackTrace(oos);
154         oos.flush();
155         oos.close();
156 
157         log.debug(baos.toString());
158     }
159 }