View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.datacollection.agent;
20  
21  
22  import org.apache.hadoop.chukwa.datacollection.adaptor.Adaptor;
23  import org.apache.log4j.Logger;
24  
25  /**
26   * Produces new unconfigured adaptors, given the class name of the appender type.
27   *  Will try the name both in the default package, and then with 
28   *  'org.apache.hadoop.chukwa.datacollection.adaptor' prepended.
29   *  
30   */
31  public class AdaptorFactory {
32    public static final String PREPENDED_PACKAGE = "org.apache.hadoop.chukwa.datacollection.adaptor.";
33    static Logger log = Logger.getLogger(ChukwaAgent.class);
34  
35    /**
36     * Instantiate an adaptor that can be added by the {@link ChukwaAgent}
37     * 
38     * @param className the name of the {@link Adaptor} class to instantiate
39     * @return an Adaptor of the specified type
40     */
41    static public Adaptor createAdaptor(String className) {
42      Object obj = null;
43      try {
44        // the following reflection business for type checking is probably
45        // unnecessary
46        // since it will just throw a ClassCastException on error anyway.
47        obj = Class.forName(className).newInstance();
48        if (Adaptor.class.isInstance(obj)) {
49          return (Adaptor) obj;
50        } else
51          return null;
52      } catch (Exception e1) {
53        log.debug("Error instantiating new adaptor by class name, "
54                + "attempting again, but with default chukwa package prepended, i.e. "
55                + PREPENDED_PACKAGE + className
56                + ". " + e1);
57        try {
58          // if failed, try adding default class prefix
59          Object obj2 = Class.forName(
60              PREPENDED_PACKAGE + className)
61              .newInstance();
62          if (Adaptor.class.isInstance(obj2)) {
63            log.debug("Succeeded in finding class by adding default adaptor "
64                + "namespace prefix to class name profided");
65            return (Adaptor) obj2;
66          } else
67            return null;
68        } catch (Exception e2) {
69          log.warn("Error instantiating new adaptor "+ className +  " by classname"
70              + " and also with \"o.a.h.c.datacollection.adaptor\" prefix added", e2);
71          return null;
72        }
73      }
74    }
75  
76  }