Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 148203

Summary: [deployables] ArrayIndexOutOfBoundsException in ComponentDeployable
Product: [WebTools] WTP Java EE Tools Reporter: Gunnar Wagenknecht <gunnar>
Component: wst.webAssignee: John Lanuti <jlanuti>
Status: CLOSED FIXED QA Contact: Chuck Bridgham <cbridgha>
Severity: critical    
Priority: P2 CC: cbridgha, david_williams
Version: 1.5   
Target Milestone: 1.5 RC6   
Hardware: All   
OS: All   
Whiteboard: PMC_approved
Attachments:
Description Flags
Proposed patch
none
screeenshot of variables view with bogus module folders
none
Proposed patch none

Description Gunnar Wagenknecht CLA 2006-06-22 08:52:36 EDT
1.5 RC5

The following exception occured while publishing to the server. I think it's due to the changes in bug 145090

ENTRY org.eclipse.wst.server.core 4 0 2006-06-22 08:43:33.187
!MESSAGE Could not publish to the server.
!STACK 0
java.lang.ArrayIndexOutOfBoundsException: 3
        at
org.eclipse.wst.web.internal.deployables.ComponentDeployable.getExistingModuleResource(ComponentDeployable.java:250)
        at
org.eclipse.wst.web.internal.deployables.ComponentDeployable.getExistingModuleResource(ComponentDeployable.java:252)
        at
org.eclipse.wst.web.internal.deployables.ComponentDeployable.getExistingModuleResource(ComponentDeployable.java:252)
        at
org.eclipse.wst.web.internal.deployables.ComponentDeployable.getMembers(ComponentDeployable.java:126)
        at
org.eclipse.wst.web.internal.deployables.ComponentDeployable.getMembers(ComponentDeployable.java:135)
        at
org.eclipse.jst.j2ee.internal.deployables.J2EEFlexProjDeployable.members(J2EEFlexProjDeployable.java:133)
        at
org.eclipse.wst.server.core.internal.ModulePublishInfo.fillCache(ModulePublishInfo.java:212)
        at
org.eclipse.wst.server.core.internal.ModulePublishInfo.getDelta(ModulePublishInfo.java:261)
        at
org.eclipse.wst.server.core.internal.ServerPublishInfo.getDelta(ServerPublishInfo.java:250)
        at
org.eclipse.wst.server.core.internal.Server.getPublishedResourceDelta(Server.java:967)
        at
org.eclipse.wst.server.core.model.ServerBehaviourDelegate.getPublishedResourceDelta(ServerBehaviourDelegate.java:491)
        at
org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:555)
        at
org.eclipse.wst.server.core.internal.Server.doPublish(Server.java:803)
        at org.eclipse.wst.server.core.internal.Server.publish(Server.java:791)
        at
org.eclipse.wst.server.core.internal.PublishServerJob.run(PublishServerJob.java:145)
        at org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)
Comment 1 Gunnar Wagenknecht CLA 2006-06-22 08:55:07 EDT
I looked into the code and the problematic line contains:

   pathSegments[moduleSegments.length > 0 ? moduleSegments.length : 0]

However, the code does not check if pathSegments.length > moduleSegments.length. I'm also curious if the last check-in broke it.

http://dev.eclipse.org/viewcvs/index.cgi/wst/components/web/plugins/org.eclipse.wst.web/static_web_project/org/eclipse/wst/web/internal/deployables/ComponentDeployable.java.diff?r1=1.10&r2=1.11&cvsroot=WebTools_Project

Comment 2 Gunnar Wagenknecht CLA 2006-06-22 08:58:52 EDT
According to the check-in comment the suspicious change was made to fix bug 119286.
Comment 3 John Lanuti CLA 2006-06-22 09:35:12 EDT
Created attachment 45084 [details]
Proposed patch

Here is a proposed patch to add the greater than check.  Can you test with this patch?
Comment 4 John Lanuti CLA 2006-06-22 09:37:20 EDT
Patch attached.  Added a safe check to ensure we don't get an array index out of bounds exception.  This code was recently reworked to improve performance and passed our scenarios, so not sure what is different about Gunnar's.  At any rate, this change makes sense and is very safe and contained.
Comment 5 Gunnar Wagenknecht CLA 2006-06-22 10:30:00 EDT
I know about the performance improvements and that's why I think the code needs some additional changes instead of just avoiding the AIOOBE.

I debugged through my case when the AIOOBE is thrown and the interesting thing is that pathSegments.length == moduleSegments.length. If this happens and startsWith(...) returns true then the paths are equal. That also means that the method must return this resource because it was explicitly asked for it.

Just for the record, the path#toString() is "WEB-INF/classes/aSubfolder" and moduleResource#toString() is "ModuleFolder [aSubfolder, WEB-INF/classes/aSubfolder]".

So, the additional check to avoid the AIOOBE is ok. But there must be a problem in the preceding code (line 238-244).

// If the last segment in passed in path equals the module resource name
// and segment count is the same and the path segments start with the module path segments
// then we have a match and we return the existing moduleResource
if (pathSegments[pathSegments.length - 1].equals(moduleResource.getName()) &&
        (moduleSegments.length + 1) == pathSegments.length &&
        startsWith(moduleSegments, pathSegments))
    return moduleResource;


The comment clearly stated "and segment count is the same". This clashes with the check "(moduleSegments.length + 1) == pathSegments.length". Either I have a suspicious moduleResource or the check should be simply moduleSegments.length == pathSegments.length.
Comment 6 Gunnar Wagenknecht CLA 2006-06-22 10:39:53 EDT
Some more debugging info about the module resource:

ModuleResource#container = F/myProject/build/war/classes/aSubFolder
ModuleResource#members = null
ModuleResource#name = aSubFolder
ModuleResource#path = WEB-INF/classes/aSubFolder

"build/war/classes" is the output directory from the JDT compiler. Thus, "aSubFolder" is actually a start folder of package hierarchy.
Comment 7 Gunnar Wagenknecht CLA 2006-06-22 11:00:01 EDT
I'm not sure if I have a bogus module resource. For most resources the expression (moduleSegments.length + 1) == pathSegments.length is true.
Comment 8 John Lanuti CLA 2006-06-22 12:16:11 EDT
 "ModuleFolder [aSubfolder, WEB-INF/classes/aSubfolder]" means that there is a folder called "aSubFolder" nested in the "aSubFolder" folder in "WEB-INF/classes". So, this case should fail.  It is trying to find "WEB-INF/classes/aSubfolder/aSubFolder".  Does this resource exist? That's what it is trying to do.
Comment 9 Gunnar Wagenknecht CLA 2006-06-22 12:22:00 EDT
(In reply to comment #8)
>  "ModuleFolder [aSubfolder, WEB-INF/classes/aSubfolder]" means that there is a
> folder called "aSubFolder" nested in the "aSubFolder" folder in
> "WEB-INF/classes". So, this case should fail.  It is trying to find
> "WEB-INF/classes/aSubfolder/aSubFolder".  Does this resource exist? That's what
> it is trying to do.

A folder (or file) with the path "WEB-INF/classes/aSubfolder/aSubFolder" does not exist. According to the path passed to getExistingModuleResource it is trying to find "WEB-INF/classes/aSubfolder".

During debugging I realized that this happens for different sub folders, too.





Comment 10 Gunnar Wagenknecht CLA 2006-06-22 12:36:51 EDT
Created attachment 45095 [details]
screeenshot of variables view with bogus module folders

See the attached screenshot. I created a simple detail formatter to print out the hierarchy. It looks like someone generates bogus module folders.
Comment 11 Gunnar Wagenknecht CLA 2006-06-22 12:38:37 EDT
(In reply to comment #10)
> Created an attachment (id=45095) [edit]
> screeenshot of variables view with bogus module folders

BTW, this screenshot is from a stack frame before the one that processes the list of resources and throws the exception.
Comment 12 John Lanuti CLA 2006-06-22 12:56:05 EDT
Gunnar, Can you attach a zip of your project or a similar project exposing this issue?
Comment 13 John Lanuti CLA 2006-06-22 13:09:38 EDT
I am not able to reproduce this behaviour on various web projects.
Comment 14 Gunnar Wagenknecht CLA 2006-06-22 14:18:47 EDT
(In reply to comment #12)
> Gunnar, Can you attach a zip of your project or a similar project exposing this
> issue?

I sent you a workspace to your mail address. I was able to reproduce the problem there.

Comment 15 John Lanuti CLA 2006-06-22 14:57:10 EDT
Gunnar, can you just export the project and send it to me or attach here?
Comment 16 Gunnar Wagenknecht CLA 2006-06-22 15:03:37 EDT
It's not just a project. You need the workspace (contains two projects) I sent you. It's a stripped down version (without confidential code, just a few test classes) that is exposing the problem.

I can't attach the workspace here, sorry.
Comment 17 John Lanuti CLA 2006-06-22 15:32:41 EDT
Created attachment 45116 [details]
Proposed patch

Hey Gunnar, I was able to reproduce with your test and you're right, it looks like a pretty basic problem with creating the module resources.  I added an updated patch which works in my testing, can you give it a shot?

Thanks for the effort here!
Comment 18 Gunnar Wagenknecht CLA 2006-06-22 15:40:37 EDT
(In reply to comment #17)
> Created an attachment (id=45116) [edit]
> Proposed patch

The patch works fine. I could also verify that the bogus folders are gone.

Thanks!
Comment 19 John Lanuti CLA 2006-06-22 15:53:44 EDT
Great, thanks Gunnar.  The problem here was that the java folder module resources were getting create with a path with an extra segment.  That is, its parent container was set to be itself.  That created some confusion.  The fix is trivial, set the parent path to be the parent.  It is just a one word change and myself and Gunnar have verified it works.  I have tested other scenarios and junits.

This bug is ready for PMC approval.
Comment 20 John Lanuti CLA 2006-06-22 15:59:56 EDT
We will also add this particular test to our junit test bucket for deployables.
Comment 21 David Williams CLA 2006-06-22 16:01:53 EDT
+1 

As I understand it, the main problem was a "typo" where 'newpath' was used instead of 'path' Plus, the patch includes some bullet proofing to gaurd the range can not get out of bound. 

Thanks all. 

Comment 22 John Lanuti CLA 2006-06-22 16:11:42 EDT
This is released for the WTP 1.5 RC6 062206 driver.
Comment 23 John Lanuti CLA 2006-06-22 16:13:47 EDT
Dropped.
Comment 24 Jason Sholl CLA 2006-09-26 12:48:20 EDT
verified 20060925
Comment 25 John Lanuti CLA 2006-09-26 15:03:34 EDT
Verified, closing.