cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Run servlet code in method context?

tbusch
1-Newbie

Run servlet code in method context?

Hey guys,

I've got a servlet running on Windchill's Tomcat instance which doesn't seem to think it's actually part of windchill -- I'm getting the "No active method context" message when I try to call getViewContentURL() on an ApplicationData object. Can anyone clue me in on how to set up the proper method context from here?

public static JSONObject getPartPDF(String[] params) {
JSONObject toReturn = new JSONObject();
if(params.length < 1) {
toReturn.accumulate("error", "Must provide Object ID!");
return toReturn;
}

String obid = params[0];

try {
Persistable p = DocumentUtility.getObjectFromObid(obid);

String partNumber = AttributeUtility.getAttributeValue((IBAHolder) p, "Part Number");
if(partNumber != null && !"null".equals(partNumber)) {
ApplicationData ad = ContentUtility.getAttachment((ContentHolder) p, partNumber + ".pdf");
if(ad != null) {
toReturn.put("url", ad.getViewContentURL((ContentHolder) p)); // ***** Error here! *****
toReturn.put("partNumber", partNumber);
toReturn.put("obid", obid);
}
}
} catch (WTException e) {
e.printStackTrace();
toReturn.put("error", e.getMessage());
}

return toReturn;
}

4 REPLIES 4

<jsp:usebean id="wtcontext" class="wt.httpgw.WTContextBean"&lt;br"/>scope="request"/>

<jsp:setproperty name="wtcontext" property="request" value="&lt;%=request%">"/>

hth, martin

tbusch
1-Newbie
(To:tbusch)

So that translates to this in a sevlet:

WTContextBean wtcontext = (WTContextBean) request.getAttribute("wtcontext");
if(wtcontext == null) wtcontext = new WTContextBean();
wtcontext.setRequest(request);

Does that seem correct?

-Thomas R. Busch
Sr. Software Developer
Stryker Instruments
(269) 323-7700 x4014
tom.busch@stryker.com<">mailto:tom.busch@stryker.com>
jessh
5-Regular Member
(To:tbusch)

That looks correct as far as establishing a WTContext is concerned.

Note that this is unnecessary (but harmless) in recent versions of
Windchill (R9.x) as these do this for you in a servlet filter that is
applied to the Windchill entire web app.

If a WTContext is properly set up and you get a "no active method
context" message, then it is likely that you're calling an API that is
designed to only run within the method server (which is the only place
one can get an active method context) someplace else. In this case
you'll need to make an xxxxHelper.service.yyyy() or
xxxxHelper.manager.yyyy() call to do the work for your in the method
server. [In R10 the servlet engine runs within the method server, so
you don't have to worry about such things.]

If you're in a method server and get a "no active method context"
message, then this is normally a sign that you created your own thread
to do work in and neglected to establish a method context around calls
you make to Windchill APIs. [If you're doing this sort of thing, be
sure to unregister the method context in a finally block as well...]

--
Jess Holle
>
> So that translates to this in a sevlet:
>
> WTContextBean wtcontext = (WTContextBean)
> request.getAttribute("wtcontext");
>
> *if*(wtcontext == *null*) wtcontext = *new* WTContextBean();
>
> wtcontext.setRequest(request);
>
> Does that seem correct?
>
> -Thomas R. Busch
>
> Sr. Software Developer
>
> Stryker Instruments
>
> (269) 323-7700 x4014
>
> tom.busch@stryker.com <">mailto:tom.busch@stryker.com>
>
> *From:* martin kraegeloh [
> <jsp:setproperty name="wtcontext" property="request" <br="/>> value="<%=request%>"/>
>
> hth, martin
>
>
jessh
5-Regular Member
(To:tbusch)

> That looks correct as far as establishing a WTContext is concerned.
>
> Note that this is unnecessary (but harmless) in recent versions of
> Windchill (R9.x) as these do this for you in a servlet filter that is
> applied to the Windchill entire web app.
I should also note that if your code is running in a thread other than a
servlet request thread you'll need set up your own WTContext in R9.x as
well. R9.x only handles the servlet request threads for you -- which is
normally all you need.

--
Jess Holle

Top Tags