001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.snmp.util;
018
019 import java.beans.PropertyEditorSupport;
020 import java.io.BufferedReader;
021 import java.io.FileReader;
022 import java.io.IOException;
023 import java.util.StringTokenizer;
024
025 import org.snmp4j.smi.OID;
026 import org.springframework.core.io.DefaultResourceLoader;
027 import org.springframework.core.io.Resource;
028
029 /**
030 * @author lhein
031 */
032 public class OIDListEditor extends PropertyEditorSupport {
033
034 /* (non-Javadoc)
035 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
036 */
037 @Override
038 public void setAsText(String text) throws IllegalArgumentException {
039 OIDList list = new OIDList();
040 if (text.indexOf(",") != -1) {
041 // seems to be a comma separated oid list
042 StringTokenizer strTok = new StringTokenizer(text, ",");
043 while (strTok.hasMoreTokens()) {
044 String tok = strTok.nextToken();
045 if (tok != null && tok.trim().length()>0) {
046 list.add(new OID(tok.trim()));
047 } else {
048 // empty token - skip
049 }
050 }
051 } else if (text.indexOf(":") != -1) {
052 // seems to be a file resource
053 try {
054 DefaultResourceLoader loader = new DefaultResourceLoader();
055 Resource file = loader.getResource(text);
056
057 if (file.exists()) {
058 BufferedReader br = null;
059 try {
060 br = new BufferedReader(new FileReader(file.getFile()));
061 String line = null;
062 while ((line = br.readLine()) != null) {
063 list.add(new OID(line.trim()));
064 }
065 } catch (IOException ex) {
066 throw new IllegalArgumentException(text + " is not a valid argument.", ex);
067 } finally {
068 if (br != null) {
069 try {
070 br.close();
071 } catch (IOException ex) {
072 // ignore
073 }
074 }
075 }
076 } else {
077 // the specified resource file is not existing
078 throw new IllegalArgumentException(text + " is not a valid argument.");
079 }
080 } catch (Exception ex) {
081 throw new IllegalArgumentException(text + " is not a valid argument.", ex);
082 }
083 } else {
084 // maybe a single oid
085 list.add(new OID(text.trim()));
086 }
087 setValue(list);
088 }
089 }