[SOLVED] Scipting assistance to search for servlets in xml file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Scipting assistance to search for servlets in xml file
Hello all,
We're migrating from Tomcat on Linux to Glassfish on Windows and I'm trying to make it easier to find the servlets that we don't currently have implicitly mapped in the Tomcat web.xml files for contexts. What I've done is copy all our contexts to a temp directory and written a script so that I can loop through each context, find the files that are not mapped inside a package then check if the java file is a servlet or not. From there I store each java file that matches, strip off the extension and then search in the context web.xml for a match then output to the screen.
The problem that I'm running into is that I know there are class files that should be returned and yet they are not being displayed. I'm not a Linux guy, so I'm just looking for a little help to see if I'm missing something obvious. I've attached the code for the script below. Thanks.
Code:
# for all contexts
for context in `ls "/var/tomcat/temp/script/" | grep -v servletmap.sh`
do
echo $context
# for all java files without a package
for file in `ls "/var/tomcat/temp/script/$context/src/java/" | grep .java`
do
#check to see if it is a servlet.
if egrep -q "(protected|public) void do(Get|Post)" /var/tomcat/temp/script/$context/src/java/$file
then
echo
else
class=`echo $file | sed s/.java//`
#check to see if it is declared in web.xml
if grep -q $class /var/tomcat/temp/script/$context/web/WEB-INF/web.xml
then
echo " $class"
fi
fi
done
done
Last edited by XavierP; 07-18-2011 at 01:05 PM.
Reason: Code tags and moved to Programming
I changed up the script a little to have some additional output (removed protected portion of the egrep). Also changed the servlet names to protect the innocent. Apologies for all the white space.
In these cases I know they are all correct, because servlets 1-5 are protected methods, not public, even though they are in the xml. The problem I'm having is that there are servlets that qualify under egrep portion, yet aren't declared in the xml and I don't get those as results.
What we need to see is the input .java file that has the protected and public methods that are not being captured by the egrep?
Also, I think you could simplify your code a little:
Code:
for FILE in /var/tomcat/temp/script/*/src/java/*.java
do
if ! egrep -q "(protected|public) void do(Get|Post)" $FILE
then
CLASS=${FILE##*/}
CLASS=${CLASS%.java}
XML=${FILE%src*}web/WEB-INF/web.xml
if grep -q "$CLASS" $XML
then
echo " $CLASS"
fi
fi
done
I didn't see any reason to put in the superfluous echo which has created your original output. If you have a valid
reason then simply add an else to the above and you should get the same desired affect.
Here's the sample servlet code snippet for a servlet not being captured by the script that is not contained in a web.xml file:
Code:
public class AP_CostCenter extends HttpServlet {
protected FileUtility fu = new FileUtility();
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
res.setContentType("application/vnd.ms-excel");
PrintWriter out = res.getWriter();
That's where I was going wrong. Moving the exclusion to the grep statement searching the web.xml files gave me the results that I was hoping to get. I appreciate your help on this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.