001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.webwork;
034:
035: import java.io.*;
036: import java.text.*;
037: import java.util.*;
038: import javax.servlet.http.*;
039:
040: import com.opensymphony.webwork.interceptor.*;
041: import org.apache.oro.text.perl.*;
042:
043: import com.jeantessier.classreader.*;
044: import com.jeantessier.dependency.*;
045: import com.jeantessier.dependencyfinder.*;
046:
047: public class ExtractAction extends ActionBase implements
048: ServletResponseAware {
049: private boolean update;
050:
051: private PrintWriter out = new NullPrintWriter();
052:
053: public void setUpdate(boolean update) {
054: this .update = update;
055: }
056:
057: public boolean getUpdate() {
058: return update;
059: }
060:
061: public void setServletResponse(HttpServletResponse response) {
062: try {
063: out = new PrintWriter(new OutputStreamWriter(response
064: .getOutputStream()));
065: } catch (IOException e) {
066: // Ignore
067: }
068: }
069:
070: public String execute() throws Exception {
071: return SUCCESS;
072: }
073:
074: public String doDefault() {
075: return INPUT;
076: }
077:
078: public String doUpdate() {
079: Date start = new Date();
080:
081: extractGraph();
082:
083: Date stop = new Date();
084: double duration = (stop.getTime() - start.getTime())
085: / (double) 1000;
086:
087: SimpleDateFormat formatter = new SimpleDateFormat(
088: "yyyy-MM-dd HH:mm:ss");
089:
090: application.put("updateStart", formatter.format(start));
091: application.put("updateDuration", duration);
092:
093: application.remove("loadStart");
094: application.remove("loadDuration");
095:
096: return SUCCESS;
097: }
098:
099: public String doExtract() {
100: Date start = new Date();
101:
102: dispatcher = new ModifiedOnlyDispatcher(
103: ClassfileLoaderEventSource.DEFAULT_DISPATCHER);
104: factory = new NodeFactory();
105: CodeDependencyCollector collector = new CodeDependencyCollector(
106: factory);
107: DeletingVisitor deletingVisitor = new DeletingVisitor(factory);
108:
109: monitor = new Monitor(collector, deletingVisitor);
110:
111: extractGraph();
112:
113: Date stop = new Date();
114: double duration = (stop.getTime() - start.getTime())
115: / (double) 1000;
116:
117: SimpleDateFormat formatter = new SimpleDateFormat(
118: "yyyy-MM-dd HH:mm:ss");
119:
120: application.put("dispatcher", dispatcher);
121: application.put("factory", factory);
122: application.put("monitor", monitor);
123:
124: application.put("extractStart", formatter.format(start));
125: application.put("extractDuration", duration);
126: application.remove("updateStart");
127: application.remove("updateDuration");
128:
129: application.remove("loadStart");
130: application.remove("loadDuration");
131:
132: return SUCCESS;
133: }
134:
135: private void extractGraph() {
136: Perl5Util perl = new Perl5Util();
137: Collection<String> sources = new LinkedList<String>();
138: perl.split(sources, "/,\\s*/", source);
139:
140: VerboseListener listener = new VerboseListener(out);
141:
142: ClassfileLoader loader = new TransientClassfileLoader(
143: dispatcher);
144: loader.addLoadListener(listener);
145: loader.addLoadListener(monitor);
146: loader.load(sources);
147:
148: if ("maximize".equalsIgnoreCase(mode)) {
149: listener.print("Maximizing ...");
150: new LinkMaximizer().traverseNodes(factory.getPackages()
151: .values());
152: } else if ("minimize".equalsIgnoreCase(mode)) {
153: listener.print("Minimizing ...");
154: new LinkMinimizer().traverseNodes(factory.getPackages()
155: .values());
156: }
157: }
158: }
|