百度空间 | 百度首页 
 
查看文章
 
Using external libraries in Webdynpro for Java
2009-03-31 21:25

Introduction

There are a lot of messages on SDN forums regarding using third-party libraries in SAP J2EE applications. Also there is a complete section available at http://help.sap.com, many developers either find it incomplete or cannot find it at all (I bet majority even don’t try to search, but this is different topic).

Below is step-by-step guide how to deploy Rhino JavaScript interpreter library to J2EE server and use it in WebDynpro application.

Step 0. Preparation

First download latest Rhino build from mozilla.org and extract <var>js.jar</var> file from Zip archive. Mozilla download page contains broken links, here is direct link to Rhino 1.6 R2 / RC2.

Next, activate "Development Configurations" perpective in NetWeaver IDE (use menu path: "Window -> Open Perpective -> Other..."). We will create all necessary development components (DC for short) in Local Development / My Components branch.

image

Activate "Development Configurations" view.

Step 1. Create and build “External Library”

Switch to "Development Configurations" perpective if you are not already there, drill down to "Local Development -> My Components", right-click and select "Create New DC..." command

image

Running "Create New DC..." dialog.

Now fill in all necessary fields as shown below and click finish.

image

Create "External Library" DC.

IDE will create and open automatically necessary project. In root project directory find a folder <var>libraries</var> (in my environment NetWeaver shows it in project tree, but does not create it on disk - just create it manually). Now copy <var>js.jar</var> from Rhino distribution here. Next, select <var>library</var> folder in IDE, right-click on it and call "Refresh" command.

After IDE synchronized changes with file system, right-click on jar file in tree and select "Development Component -> Add To Public Part"

image

Adding Aarchive (js.jar) to public part

NetWeaver pre-create <var>ExternalLibs</var> public part for this type of projects, so simply select it in appeared dialog and click "OK". Afterwards right-click on project itself and run command "Development Component -> Build". Step complete.

Step 2. Create, build, deploy “J2EE Server / Library”

After we have created and built <var>External Library</var> DC we need to deploy it to SAP J2EE server somehow. So next create "J2EE Server Component / Library" DC:

image

Create "J2EE Server Component / Library" DC

Note that during project createion NetWeaver IDE may prompt you to open <var>JEE Development Perspective</var>. Just agree with it. Along this it will open <var>J2EE DC Explorer</var> view (switch to this view yourself if this doesn't happen). Now expand project structure to "DC Metadata -> DC Definition -> Used DCs". Activate contextual menu on last node and invoke "Add Used DC...". Add reference to the public part of previously created <var>Extrenal Library</var> here. Note, that you have to specify both build time and run-time dependency here. Otherwise created SDA file will not contain js.jar from external library.

image

Add used DC (External Library) to J2EE library

It is time to build the project and analyze results. First of all you have to check generated SDA file in <var>gen/default/deploy</var> folder. Verify that it contains <var>js.jar</var>. Now something interesting. Go to <var>gen/default/public</var> folder. You should find here <var>defLib</var> folder. Yes, NetWeaver IDE again created public part, this time it is <var>defLib</var>. And if you drill down to <var>gen/default/plublic/defLib/lib/java</var> then you meet <var>js.jar</var> file again.

From now you may refere this (defLib) public part in the rest of SAP J2EE projects. But before we have to deploy it!. So, context menu on SAP J2EE Component / Library project, "Development Component -> Deploy". Everything should go smoothly, let us verify results and run Visual Administrator:

image

Scroll to Libraries node
image

JavaScript (Rhino) Library Deployed

Step 3. Create WebDynpro Development Component

Now you may refer JavaScript library from any J2EE project. Obviously, that I wiil use it from WebDynpro ;)

So, two initial tasks should be known for you: we will create WebDynpro Development Component and add <var>defLib</var> public part of <var>js/lib</var> DC to it (the only difference is that here build-time dependency is enough):

image

Create "WebDynpro" DC.
image

Add used DC (defLib) to WebDynpro DC

However, WebDynpro project will not contain JavaScript library files (as in case with J2EE library). So we have to define <var>reference</var> to aforementioned library to let class loader find necessary classes at run-time. Run contextual menu on project root in IDE and select "Properties", then switch to "WebDynpro References -> Library references" and add "mozilla.org~js~lib" (you may see the name in Visual Administrator):

image

Add run-time library reference for WebDynpro DC

Back to component itself. It is extremely trivial: single view with 2 IWDTextArea-s for JavaScript source and script output, one button and corresponding action handler. Here is the code of action handler:

final Context ctx = Context.enter();
try
{
final Scriptable scope = new ImporterTopLevel(ctx);

// Export "out" to scope
final ByteArrayOutputStream result = new ByteArrayOutputStream(2048);
final PrintStream out = new PrintStream( result );
final Object wout = Context.javaToJS(out, scope);
ScriptableObject.putProperty( scope, "out", wout );

try
{
ctx.evaluateString
(
scope, // Initial scope
wdContext.currentContextElement().getJavaScript(), // JS source
"<cmd>", // File name in error messages
1, // First line to execute
null // Security manager
);
}
finally
{
out.close();
}
wdContext.currentContextElement().setOutput
(
new String( result.toByteArray() )
);
}
catch (final Exception e)
{
final ByteArrayOutputStream error = new ByteArrayOutputStream(2048);
final PrintStream out = new PrintStream( error );
e.printStackTrace( out );
out.close();
wdContext.currentContextElement().setOutput
(
new String(error.toByteArray())
);
wdComponentAPI.getMessageManager().reportException
(
new WDNonFatalException(e), false
);
}
finally
{
Context.exit();
}

And, finally, here is running application:

image

JavaScript Console

Heck, we "invent" tool before task ;) If anyone has ideas how scripting could be applied in WebDynpro you are welcome to share your thoughts in comments!

From: https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/2361


类别:Sap | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu