001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.automate;
027:
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: /**
034: *
035: *
036: * @author $author$
037: * @version $Revision: 1.13 $
038: */
039: public class RemoteIdentificationRule {
040: private static HashSet allowedOperations = new HashSet();
041:
042: static {
043: allowedOperations.add("startsWith");
044: allowedOperations.add("contains");
045: }
046:
047: private HashMap expressions = new HashMap();
048: private int priority = 10;
049: private String name;
050:
051: /**
052: *
053: *
054: * @param identification
055: *
056: * @return
057: */
058: public boolean testRule(String identification) {
059: // Get the software version portion of the id string
060: String svc = identification.substring(identification
061: .lastIndexOf("-") + 1);
062: Iterator it = expressions.entrySet().iterator();
063: Map.Entry entry;
064: boolean pass = false;
065: String operation;
066:
067: while (it.hasNext()) {
068: entry = (Map.Entry) it.next();
069: operation = (String) entry.getKey();
070:
071: if (operation.equals("startsWith")) {
072: pass = svc.startsWith((String) entry.getValue());
073: }
074:
075: if (operation.equals("contains")) {
076: pass = (svc.indexOf((String) entry.getValue()) >= 0);
077: }
078: }
079:
080: return pass;
081: }
082:
083: /**
084: *
085: *
086: * @param priority
087: */
088: protected void setPriority(int priority) {
089: this .priority = priority;
090: }
091:
092: /**
093: *
094: *
095: * @param operation
096: * @param value
097: *
098: * @throws UnsupportedRuleException
099: */
100: protected void addExpression(String operation, String value)
101: throws UnsupportedRuleException {
102: if (allowedOperations.contains(operation)) {
103: expressions.put(operation, value);
104: } else {
105: throw new UnsupportedRuleException("The rule '" + operation
106: + "' is not supported");
107: }
108: }
109:
110: /**
111: *
112: *
113: * @param name
114: */
115: protected void setName(String name) {
116: this .name = name;
117: }
118:
119: /**
120: *
121: *
122: * @return
123: */
124: public String getName() {
125: return name;
126: }
127:
128: /**
129: *
130: *
131: * @return
132: */
133: public int getPriority() {
134: return priority;
135: }
136: }
|