001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - DefaultMOQuery.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent;
022:
023: // For JavaDoc
024: import org.snmp4j.agent.request.Request;
025:
026: /**
027: * The <code>DefaultMOQuery</code> class is the default implementation of a
028: * managed object query. It is used to lookup managed objects, for example in
029: * a {@link MOServer} repository.
030: *
031: * @author Frank Fock
032: * @version 1.1
033: */
034: public class DefaultMOQuery implements MOQuery {
035:
036: private MOContextScope scope;
037: private boolean writeAccessQuery;
038: private Object source;
039:
040: /**
041: * Creates a context aware query from a context aware OID scope.
042: * @param scope
043: * a scope that defines the possible result set of OIDs from a specific
044: * context for this query.
045: */
046: public DefaultMOQuery(MOContextScope scope) {
047: this .scope = scope;
048: }
049:
050: /**
051: * Creates a context aware query from a context aware OID scope.
052: * @param scope
053: * a scope that defines the possible result set of OIDs from a specific
054: * context for this query.
055: * @param isWriteAccessIntended
056: * indicates whether this query serves a write access on
057: * {@link ManagedObject}s or not.
058: * @since 1.1
059: */
060: public DefaultMOQuery(MOContextScope scope,
061: boolean isWriteAccessIntended) {
062: this (scope);
063: this .writeAccessQuery = isWriteAccessIntended;
064: }
065:
066: /**
067: * Creates a context aware query from a context aware OID scope.
068: * @param scope
069: * a scope that defines the possible result set of OIDs from a specific
070: * context for this query.
071: * @param isWriteAccessIntended
072: * indicates whether this query serves a write access on
073: * {@link ManagedObject}s or not.
074: * @since 1.1
075: */
076: public DefaultMOQuery(MOContextScope scope,
077: boolean isWriteAccessIntended, Object source) {
078: this (scope, isWriteAccessIntended);
079: this .source = source;
080: }
081:
082: /**
083: * Gets the search range of this query.
084: *
085: * @return a <code>MORange</code> instance denoting upper and lower bound of
086: * this queries scope.
087: */
088: public MOContextScope getScope() {
089: return scope;
090: }
091:
092: /**
093: * Checks whether a managed object matches the internal query criteria
094: * defined by this query.
095: *
096: * @param managedObject the <code>ManagedObject</code> instance to check.
097: * @return <code>true</code> if the <code>managedObject</code> matches the
098: * query.
099: */
100: public boolean matchesQuery(ManagedObject managedObject) {
101: return true;
102: }
103:
104: public void substractScope(MOScope scope) {
105: if (this .scope instanceof MutableMOScope) {
106: ((MutableMOScope) this .scope).substractScope(scope);
107: } else {
108: throw new UnsupportedOperationException();
109: }
110: }
111:
112: public String toString() {
113: return getClass().getName() + "[" + getScope().getContext()
114: + "]=" + getScope().getLowerBound() + "<"
115: + (getScope().isLowerIncluded() ? "=" : "") + " x <"
116: + (getScope().isUpperIncluded() ? "=" : "")
117: + getScope().getUpperBound();
118: }
119:
120: public boolean isWriteAccessQuery() {
121: return writeAccessQuery;
122: }
123:
124: /**
125: * Gets the source ({@link Request}) object on whose behalf this query is
126: * executed. This object reference can be used to determine whether a query
127: * needs to update {@link ManagedObject} content or not. When the reference
128: * is the same as those from the last query then an update is not necessary.
129: *
130: * @return
131: * an Object on whose behalf this query is executed which will be in most
132: * cases a {@link Request} instance, but code should not rely on that. If
133: * <code>null</code> is returned, the query source cannot be determined.
134: * @since 1.1
135: */
136: public Object getSource() {
137: return source;
138: }
139:
140: /**
141: * This method checks whether the supplied query and the given source
142: * reference refer to the same source (request).
143: *
144: * @param query
145: * a <code>MOQuery</code> instance.
146: * @param source
147: * any source object reference.
148: * @return
149: * <code>true</code> only if <code>query</code> is a
150: * <code>DefaultMOQuery</code> instance and
151: * <code>{@link DefaultMOQuery#getSource()} == source</code> and source
152: * is not <code>null</code>.
153: * @since 1.1
154: */
155: public static boolean isSameSource(MOQuery query, Object source) {
156: if (query instanceof DefaultMOQuery) {
157: return ((source != null) && (((DefaultMOQuery) query)
158: .getSource() == source));
159: }
160: return false;
161: }
162: }
|