001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.sql.SQLException;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.Properties;
030:
031: import com.pavelvlasov.jsel.statements.CompoundStatement;
032: import com.pavelvlasov.jsel.statements.EmptyStatement;
033: import com.pavelvlasov.jsel.statements.Statement;
034: import com.pavelvlasov.sql.SQLProcessor;
035: import com.pavelvlasov.util.OrderedTarget;
036: import com.pavelvlasov.util.DispatchingVisitor.Filter;
037:
038: /**
039: * @author Pavel Vlasov
040: * @version $Revision: 1.1 $
041: */
042: public class InspectorBase implements Inspector, OrderedTarget {
043: protected InspectorContext context;
044:
045: public void setContext(InspectorContext context) {
046: this .context = context;
047: }
048:
049: public void unSetContext() {
050: context = null;
051: }
052:
053: public InspectorContext getContext() {
054: return context;
055: }
056:
057: public static boolean isEmpty(Statement statement) {
058: if (statement == null) {
059: return true;
060: }
061: if (statement instanceof EmptyStatement) {
062: return true;
063: } else if (statement instanceof CompoundStatement) {
064: Iterator it = ((CompoundStatement) statement)
065: .getStatements().iterator();
066: while (it.hasNext()) {
067: if (!isEmpty((Statement) it.next())) {
068: return false;
069: }
070: }
071: return true;
072: } else {
073: return false;
074: }
075: }
076:
077: public void init() throws HammurapiException {
078: }
079:
080: public void destroy() {
081: }
082:
083: public Integer getOrder() {
084: return getContext().getDescriptor().getOrder();
085: }
086:
087: public String getConfigInfo() {
088: return null;
089: }
090:
091: /**
092: * Method from {@link com.pavelvlasov.util.DispatchingVisitor.Filter}
093: * interface. If inspector implements that interface it doesn't
094: * need to implement the method, but just provide apporve() methods.
095: * @see com.pavelvlasov.util.DispatchingVisitor.Filter#getTargets()
096: * @return Collection of targets to be filtered.
097: */
098: public Collection getTargets() {
099: return filterTargets;
100: }
101:
102: private Collection filterTargets = this instanceof Filter ? new LinkedList()
103: : null;
104:
105: public void addTarget(Object target) {
106: if (this instanceof Filter && target != null) {
107: filterTargets.add(target);
108: }
109: }
110:
111: /**
112: * Removes this inspector from invocation targets.
113: * Inspector shall invoke this method if it detects
114: * that it cannot continue functioning properly.
115: */
116: protected void disable(String message) {
117: getContext().getSession().disable(this );
118: getContext().warn(
119: null,
120: "Inspector " + getContext().getDescriptor().getName()
121: + " disabled itself with message '" + message
122: + "'");
123: }
124:
125: public void initDb(SQLProcessor processor, Properties dbProperties)
126: throws SQLException {
127:
128: }
129: }
|