Permalink
Browse files

Add table name to table configuration file

  • Loading branch information...
1 parent 236f074 commit c723719af9b55f9cf1122c667d0224d564320cc7 @uprush uprush committed Mar 18, 2012
Showing with 68 additions and 17 deletions.
  1. +22 −1 conf/tables
  2. +46 −16 framework/src/sculptor/framework/Sculptor.java
View
@@ -1 +1,22 @@
-sculptor.sample.ItemData sculptor.sample.HItemData
+# Copyright 2010 The Apache Software Foundation
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Set table, entity and hclient mapping here.
+# Format is: table_name : entity_class : client_class
+sc_item_data : sculptor.sample.ItemData : sculptor.sample.HItemData
@@ -33,46 +33,76 @@
*
*/
public class Sculptor {
-
+
private static Log log = LogFactory.getLog(Sculptor.class);
public static File sculptorRoot;
-
+
public static Map<String, Class<? extends HEntity>> entities;
public static Map<String, Class<? extends HClient>> clients;
public static Map<String, HClassDescriptor> descriptors;
-
+
+ /**
+ * Initialize Sculptor.
+ *
+ * @param root
+ * the root path of Sculptor
+ * @throws Exception
+ * Something wrong during the initialization.
+ */
public static void initialize(String root) throws Exception {
try {
sculptorRoot = new File(root);
-
+
entities = new TreeMap<String, Class<? extends HEntity>>();
clients = new TreeMap<String, Class<? extends HClient>>();
descriptors = new TreeMap<String, HClassDescriptor>();
-
+
String tables = sculptorRoot.getAbsolutePath() + "/conf/tables";
String tableDefine;
BufferedReader br = new BufferedReader(new FileReader(tables));
while ((tableDefine = br.readLine()) != null) {
- String[] clientAndEntity = tableDefine.split("\\s+");
-
+ if ("".equals(tableDefine.trim())
+ || tableDefine.startsWith("#")) {
+ // ignore empty or comment lines
+ continue;
+ }
+ String[] mapping = tableDefine.split(":");
+ if (mapping.length < 3) {
+ throw new Exception("Wrong table mapping: " + tableDefine);
+ }
+
+ // table name
+ String tableName = mapping[0].trim();
+ log.info(String.format("Loading table %s...", tableName));
+
// load entity
- Class entityClass = Class.forName(clientAndEntity[0]);
+ Class entityClass = Class.forName(mapping[1].trim());
HClassDescriptor descriptor = HEntity.getClassInfo(entityClass);
- log.info(String.format("Loading table %s...", descriptor.table));
+ if (!tableName.equals(descriptor.table)) {
+ throw new Exception(
+ String.format(
+ "Wrong HEntity table annotation, expected: %s, actual: %s",
+ tableName, descriptor.table));
+ }
// load client
- Class clientClass = Class.forName(clientAndEntity[1]);
- String tableName = ((Table) clientClass.getAnnotation(Table.class)).name();
- if (!tableName.equals(descriptor.table)) {
- throw new Exception("Entity and client implmentation should be associated with same table.");
+ String client = mapping[2].trim();
+ Class clientClass = Class.forName(client);
+ String annotationTableName = ((Table) clientClass
+ .getAnnotation(Table.class)).name();
+ if (!tableName.equals(annotationTableName)) {
+ throw new Exception(
+ String.format(
+ "Wrong HClient table annotation, expected: %s, actual: %s",
+ tableName, annotationTableName));
}
- descriptor.clientClassName = clientAndEntity[1];
-
+ descriptor.clientClassName = client;
+
entities.put(tableName, entityClass);
clients.put(tableName, clientClass);
descriptors.put(tableName, descriptor);
}
-
+
} catch (Exception e) {
log.error("Can not initialize Sculptor.", e);
throw e;

0 comments on commit c723719

Please sign in to comment.