| <!-- |
| |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| --> |
| |
| <HTML> |
| <head> |
| <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <link rel="Stylesheet" href="../prose.css" type='text/css'> |
| <title>DD API Usage</title> |
| </head> |
| <BODY> |
| <h2>The Examples of DD API Usage.</h2> |
| <h3>Example 1</h3> |
| |
| In this example, DD API client prints all listener classes from deployment descriptor to console:<br> |
| <p> |
| <pre> |
| <div class="nonnormative"> |
| <font class="keyword">package</font> mypackage; |
| |
| <font class="keyword">import</font> org.netbeans.api.web.dd.DDProvider; |
| <font class="keyword">import</font> org.netbeans.api.web.dd.WebApp; |
| <font class="keyword">import</font> org.netbeans.api.web.dd.Listener; |
| <font class="keyword">import</font> org.openide.filesystems.*; |
| |
| <font class="keyword">public class</font> <b>ListenerExample</b> { |
| <font class="comment"> |
| /** |
| * @param args the command line arguments |
| */ |
| </font> |
| <font class="keyword">public static void</font> main(String[] args)<font class="keyword">throws</font> <b>Exception</b> { |
| |
| <font class="comment">// get the file object of web.xml file</font> |
| <b>DDProvider</b> ddProvider = DDProvider.getDefault(); |
| <b>FileObject</b> fo = <b>Repository</b>.getDefault().findResource("<font color="constant">WEB-INF/web.xml</font>"); |
| |
| <font class="comment">// get the deployment descriptor root object</font> |
| <b>WebApp</b> webApp = ddProvider.getDDRoot(fo); |
| |
| <font class="comment">// print the version of deployment dscriptor</font> |
| System.out.println("<font color="constant">DD version = </font>"+webApp.getVersion()); |
| |
| <font class="comment">// get the array of listeners and print the listener classes</font> |
| <b>Listener</b> [] listeners = webApp.getListener(); |
| <font class="keyword">for</font> (int i=<font color="constant">0</font>; i<listeners.length; i++) { |
| System.out.println("<font color="constant">Listener [</font>"+i+"<font color="constant">] = </font>"+listeners[i].getListenerClass()); |
| } |
| } |
| } |
| </div> |
| </pre> |
| </p> |
| |
| <h3>Example 2</h3> |
| |
| In this example, DD API client searches a servlet "CarServlet" and, if such a servlet exists, 2 init parameters are created : "car_type" and "car_color".<br> |
| Then, finally, changes ara saved back to file object :<br> |
| <div class="nonnormative"> |
| <pre> |
| <font class="keyword">package</font> mypackage; |
| |
| <font class="keyword">import</font> org.netbeans.api.web.dd.DDProvider; |
| <font class="keyword">import</font> org.netbeans.api.web.dd.WebApp; |
| <font class="keyword">import</font> org.netbeans.api.web.dd.Servlet; |
| <font class="keyword">import</font> org.netbeans.api.web.dd.InitParam; |
| <font class="keyword">import</font> org.openide.filesystems.*; |
| |
| <font class="keyword">public class</font> <b>InitParamExample</b> { |
| <font class="comment"> |
| /** |
| * @param args the command line arguments |
| */ |
| </font> |
| <font class="keyword">public static void</font> main(String[] args) <font class="keyword">throws</font> <b>Exception</b> { |
| |
| <font class="comment">// get the file object of web.xml file</font> |
| <b>DDProvider</b> ddProvider = DDProvider.getDefault(); |
| <b>FileObject</b> fo = Repository.getDefault().findResource("<font class="constant">WEB-INF/web.xml</font>"); |
| |
| <font class="comment">// get the deployment descriptor root object</font> |
| <b>WebApp</b> webApp = ddProvider.getDDRoot(fo); |
| |
| <font class="comment">// print the version of deployment dscriptor</font> |
| System.out.println("<font class="constant">DD version = </font>"+webApp.getVersion()); |
| |
| <font class="comment">// looks for the "CarServlet" servlet at WebApp object by ServletName property</font> |
| <b>Servlet</b> servlet = (Servlet) webApp.findBeanByName("<font class="constant">Servlet</font>", "<font class="constant">ServletName</font>", "<font class="constant">CarServlet</font>"); |
| |
| if (servlet!=<font class="constant">null</font>) { |
| |
| <font class="comment">// add the first InitParam object to Servlet object</font> |
| servlet.addBean("<font class="constant">InitParam</font>", new String[]{"<font class="constant">ParamName</font>","<font class="constant">ParamValue</font>"}, new String[]{"<font class="constant">car_type</font>","<font class="constant">FORD</font>"}, <font class="constant">null</font> ); |
| |
| <font class="comment">// add the second InitParam object to Servlet object</font> |
| servlet.addBean("<font class="constant">InitParam</font>", new String[]{"<font class="constant">ParamName</font>","<font class="constant">ParamValue</font>"}, new String[]{"<font class="constant">car_color</font>","<font class="constant">green</font>"}, <font class="constant">null</font> ); |
| |
| <font class="comment">// print all init params to console</font> |
| <b>InitParam</b> [] newParams = servlet.getInitParam(); |
| <font class="keyword">for</font> (int i=<font class="constant">0</font>;i<newParams.length;i++) |
| System.out.println("<font class="constant">init-param [</font>"+i+"<font class="constant">] = </font>"+newParams[i].getParamName()+"<font class="constant"> -> </font>"+newParams[i].getParamValue()); |
| |
| <font class="comment">// write changes back to file object</font> |
| webApp.write(fo); |
| } |
| } |
| } |
| </pre> |
| </div> |
| </p> |
| |
| </BODY></HTML> |