LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2011, 12:33 PM   #1
onthevirg71
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Rep: Reputation: Disabled
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
 
Old 07-18-2011, 01:39 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

Now if you could show us sample data to go with your snippet...



Cheers,
Tink
 
Old 07-18-2011, 05:23 PM   #3
onthevirg71
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Here's sample output

Thanks Tinkster.

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.

Code:
contextname

















    Servlet1

    Servlet2




    Servlet3
    Servlet4
    Servlet5
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.
 
Old 07-18-2011, 08:26 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 07-19-2011, 10:18 AM   #5
onthevirg71
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Servlet Code Snippet

Thanks for the tip on the script grail.

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();
 
Old 07-19-2011, 03:20 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well based on what I see so far I am not sure I understand the logic?

The script says that if the following regex is not in the file:
Code:
(protected|public) void do(Get|Post)
As the snippet you have shown does have this entry:
Code:
public void doGet(HttpServletRequest req, HttpServletResponse res)
Matching portion in red means this file will be skipped.
 
1 members found this post helpful.
Old 07-19-2011, 04:07 PM   #7
onthevirg71
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
There's the mistake

Thanks grail,

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Search and Replace XML tags conditionally rammyp_1979 Programming 15 10-22-2010 09:11 AM
Need help to search pattern in xml file vishal_titre Programming 2 10-26-2008 09:24 AM
Need help to search pattern in xml file vishal_titre Programming 6 10-24-2008 02:07 PM
PHP-XML Search Engine Boby Programming 1 04-25-2005 04:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration