This project has retired. For details please refer to its Attic page.
AbstractWrapper xref
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  package org.apache.hadoop.chukwa.datacollection.adaptor;
19  
20  import java.util.regex.*;
21  import org.apache.hadoop.chukwa.Chunk;
22  import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
23  import org.apache.hadoop.chukwa.datacollection.agent.AdaptorFactory;
24  import org.apache.hadoop.chukwa.datacollection.agent.AdaptorManager;
25  
26  public class AbstractWrapper implements NotifyOnCommitAdaptor,ChunkReceiver {
27   
28    Adaptor inner;
29    String innerClassName;
30    String innerType;
31    ChunkReceiver dest;
32    AdaptorManager manager;
33    String adaptorID;
34    @Override
35    public String getCurrentStatus() {
36      return innerClassName + " " + inner.getCurrentStatus();
37    }
38  
39  
40    static Pattern p = Pattern.compile("([^ ]+) +([^ ].*)");
41    
42    /**
43     * Note that the name of the inner class will get parsed out as a type
44     */
45    @Override
46    public String parseArgs(String innerClassName, String params, AdaptorManager a) {
47      manager = a;
48      Matcher m = p.matcher(params);
49      this.innerClassName = innerClassName;
50      String innerCoreParams;
51      if(m.matches()) {
52        innerType = m.group(1);
53        inner = AdaptorFactory.createAdaptor(innerClassName);
54        innerCoreParams = inner.parseArgs(innerType,m.group(2),a);
55        return innerClassName + innerCoreParams;
56      }
57      else return null;
58    }
59    
60    @Override
61    public long shutdown(AdaptorShutdownPolicy shutdownPolicy)
62        throws AdaptorException {
63      return inner.shutdown(shutdownPolicy);
64    }
65  
66    @Override
67    public String getType() {
68      return innerType;
69    }
70  
71    /**
72     * Note that the name of the inner class will get parsed out as a type
73     */
74    @Override
75    public void start(String adaptorID, String type, long offset,
76        ChunkReceiver dest) throws AdaptorException {
77      String dummyAdaptorID = adaptorID;
78      this.dest = dest;
79      this.adaptorID = adaptorID;
80      inner.start(dummyAdaptorID, type, offset, this);
81    }
82  
83    @Override
84    public void add(Chunk event) throws InterruptedException {
85      dest.add(event);
86    }
87  
88    @Override
89    public void committed(long commitedByte) { }
90  
91  }