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