001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.identity.profile.ui.support;
043:
044: import java.io.FileInputStream;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.InputStreamReader;
049: import java.io.OutputStream;
050: import java.util.ArrayList;
051: import javax.xml.parsers.DocumentBuilder;
052: import javax.xml.parsers.DocumentBuilderFactory;
053: import javax.xml.parsers.ParserConfigurationException;
054: import javax.xml.transform.OutputKeys;
055: import javax.xml.transform.Transformer;
056: import javax.xml.transform.TransformerFactory;
057: import javax.xml.transform.dom.DOMSource;
058: import javax.xml.transform.stream.StreamResult;
059: import org.netbeans.modules.identity.profile.ui.support.J2eeProjectHelper.ProjectType;
060: import org.openide.ErrorManager;
061: import org.openide.filesystems.FileLock;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.w3c.dom.Document;
065: import org.w3c.dom.DocumentType;
066: import org.w3c.dom.Element;
067: import org.w3c.dom.Node;
068: import org.w3c.dom.NodeList;
069: import org.w3c.dom.Text;
070: import org.xml.sax.EntityResolver;
071: import org.xml.sax.InputSource;
072: import org.xml.sax.SAXException;
073:
074: /**
075: * Helper class for manipulating sun dd files.
076: *
077: * Created on August 7, 2006, 12:11 PM
078: *
079: * @author ptliu
080: */
081: public class SunDDHelper {
082: private static final String SUN_WEB_APP_TAG = "sun-web-app"; //NOI18N
083:
084: private static final String HTTPSERVLET_SECURITY_PROVIDER = "httpservlet-security-provider"; //NOI18N
085:
086: private static final String AM_HTTP_PROVIDER = "AMHttpProvider"; //NOI18N
087:
088: private static final String SECURITY_ROLE_MAPPING_TAG = "security-role-mapping"; //NOI18N
089:
090: private static final String ROLE_NAME_TAG = "role-name"; //NOI18N
091:
092: private static final String PRINCIPAL_NAME_TAG = "principal-name"; //NOI18N
093:
094: private static final String SERVLET_TAG = "servlet"; //NOI18N
095:
096: private static final String SERVLET_NAME_TAG = "servlet-name"; //NOI18N
097:
098: private static final String ENTERPRISE_BEANS_TAG = "enterprise-beans"; //NOI18N
099:
100: private static final String EJB_TAG = "ejb"; //NOI18N
101:
102: private static final String EJB_NAME_TAG = "ejb-name"; //NOI18N
103:
104: private static final String SUN_APPLICATION_CLIENT_TAG = "sun-application-client"; //NOI18N
105:
106: private static final String SERVICE_REF_TAG = "service-ref"; //NOI18N
107:
108: private static final String SERVICE_REF_NAME_TAG = "service-ref-name"; //NOI18N
109:
110: private static final String WEBSERVICE_ENDPOINT_TAG = "webservice-endpoint"; //NOI18N
111:
112: private static final String PORT_COMPONENT_NAME_TAG = "port-component-name"; //NOI18N
113:
114: private static final String PORT_INFO_TAG = "port-info"; //NOI18N
115:
116: private static final String WSDL_PORT_TAG = "wsdl-port"; //NOI18N
117:
118: private static final String NAMESPACE_URI_TAG = "namespaceURI"; //NOI18N
119:
120: private static final String LOCALPART_TAG = "localpart"; //NOI18N
121:
122: private static final String MESSAGE_SECURITY_BINDING_TAG = "message-security-binding"; //NOI18N
123:
124: private static final String MESSAGE_SECURITY_TAG = "message-security"; //NOI18N
125:
126: private static final String MESSAGE_TAG = "message"; //NOI18N
127:
128: private static final String REQUEST_PROTECTION_TAG = "request-protection"; //NOI18N
129:
130: private static final String RESPONSE_PROTECTION_TAG = "response-protection"; //NOI18N
131:
132: private static final String AUTH_LAYER_ATTR = "auth-layer"; //NOI18N
133:
134: private static final String PROVIDER_ID_ATTR = "provider-id"; //NOI18N
135:
136: private static final String AUTH_SOURCE_ATTR = "auth-source"; //NOI18N
137:
138: private static final String AM_SERVER_PROVIDER_PREFIX = "AMServerProvider-"; //NOI18N
139:
140: private static final String AM_CLIENT_PROVIDER = "AMClientProvider"; //NOI18N
141:
142: private static final String REQUEST_POLICY_AUTH_SOURCE = "content"; //NOI18N
143:
144: private static final String RESPONSE_POLICY_AUTH_SOURCE = "content"; //NOI18N
145:
146: private static final String AUTH_LAYER = "SOAP"; //NOI18N
147:
148: private static final String AUTHENTICATED_USERS = "AUTHENTICATED_USERS"; //NOI18N
149:
150: private static final String CONTEXT_ROOT_TAG = "context-root"; //NOI18N
151:
152: private static final String CLASS_LOADER_TAG = "class-loader"; //NOI18N
153:
154: private static final String SUN_WEB_SYSTEM_ID_2_4 = "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd"; //NOI18N
155:
156: private static final String SUN_WEB_SYSTEM_ID_2_5 = "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd"; //NOI18N
157:
158: private static final String SUN_EJB_SYSTEM_ID_3_0 = "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd"; //NOI18N
159:
160: private static final String SUN_APPCLIENT_SYSTEM_ID_5_0 = "http://www.sun.com/software/appserver/dtds/sun-application-client_5_0-0.dtd"; //NOI18N
161:
162: private static final String SUN_WEB_DTD_2_4 = "resources/sun-web-app_2_4-1.dtd"; //NOI18N
163:
164: private static final String SUN_WEB_DTD_2_5 = "resources/sun-web-app_2_5-0.dtd"; //NOI18N
165:
166: private static final String SUN_EJB_DTD_3_0 = "resources/sun-ejb-jar_3_0-0.dtd"; //NOI18N
167:
168: private static final String SUN_APPCLIENT_DTD_5_0 = "resources/sun-application-client_5_0-0.dtd"; //NOI18N
169:
170: private static final String IDENT = " "; //NOI18N
171:
172: private static int TIME_TO_WAIT = 300;
173:
174: private FileObject sunDD;
175: private Document document;
176: private ProjectType type;
177:
178: public SunDDHelper(FileObject sunDD, ProjectType type) {
179: this .sunDD = sunDD;
180: this .type = type;
181: }
182:
183: public void addSecurityRoleMapping() {
184: //final FileChangeListener fcl = new FileChangeAdapter() {
185: // public void fileChanged(FileEvent event) {
186: boolean isModified = false;
187: Element sunWebApp = getRootElement();
188: String value = sunWebApp
189: .getAttribute(HTTPSERVLET_SECURITY_PROVIDER);
190:
191: if (value == null || !value.equals(AM_HTTP_PROVIDER)) {
192: sunWebApp.setAttribute(HTTPSERVLET_SECURITY_PROVIDER,
193: AM_HTTP_PROVIDER);
194: isModified = true;
195: }
196:
197: Element mapping = getSecurityRoleMapping(AUTHENTICATED_USERS);
198:
199: if (mapping == null) {
200: sunWebApp
201: .appendChild(createSecurityRoleMapping(AUTHENTICATED_USERS));
202: isModified = true;
203: } else {
204: Element principal = getPrincipalName(mapping,
205: AUTHENTICATED_USERS);
206:
207: if (principal == null) {
208: mapping.appendChild(createElement(PRINCIPAL_NAME_TAG,
209: AUTHENTICATED_USERS));
210: isModified = true;
211: }
212: }
213:
214: if (isModified)
215: writeDocument();
216:
217: //sunDD.removeFileChangeListener(this);
218: //}
219: //};
220:
221: //sunDD.addFileChangeListener(fcl);
222: }
223:
224: public void removeSecurityRoleMapping() {
225: //final FileChangeListener fcl = new FileChangeAdapter() {
226: // public void fileChanged(FileEvent event) {
227: boolean isModified = false;
228: Element sunWebApp = getRootElement();
229:
230: String value = sunWebApp
231: .getAttribute(HTTPSERVLET_SECURITY_PROVIDER);
232:
233: if (value != null && value.equals(AM_HTTP_PROVIDER)) {
234: sunWebApp.removeAttribute(HTTPSERVLET_SECURITY_PROVIDER);
235: isModified = true;
236: }
237:
238: Element mapping = getSecurityRoleMapping(AUTHENTICATED_USERS);
239:
240: if (mapping != null) {
241: Element principal = getPrincipalName(mapping,
242: AUTHENTICATED_USERS);
243:
244: if (principal != null) {
245: mapping.removeChild(principal);
246: isModified = true;
247: }
248:
249: if (mapping.getElementsByTagName(PRINCIPAL_NAME_TAG)
250: .getLength() == 0) {
251: sunWebApp.removeChild(mapping);
252: isModified = true;
253: }
254: }
255:
256: if (isModified)
257: writeDocument();
258:
259: //sunDD.removeFileChangeListener(this);
260: //}
261: //};
262:
263: //sunDD.addFileChangeListener(fcl);
264: }
265:
266: public void setServiceMessageSecurityBinding(String svcDescName,
267: String pcName, String providerId) {
268: Element root = getRootElement();
269: Element component = null;
270: String componentTag = null;
271: String componentNameTag = null;
272:
273: if (type == ProjectType.WEB) {
274: componentTag = SERVLET_TAG;
275: componentNameTag = SERVLET_NAME_TAG;
276: } else if (type == ProjectType.EJB) {
277: componentTag = EJB_TAG;
278: componentNameTag = EJB_NAME_TAG;
279: }
280:
281: component = getComponentElement(root, componentTag,
282: componentNameTag, pcName);
283:
284: if (component == null) {
285: component = createComponentElement(componentTag,
286: componentNameTag, pcName);
287:
288: if (type == ProjectType.WEB) {
289: insertSunWebComponent(root, component);
290: } else {
291: root.appendChild(component);
292: }
293: }
294:
295: Element endpointElement = getElement(component,
296: WEBSERVICE_ENDPOINT_TAG);
297:
298: if (endpointElement == null) {
299: endpointElement = createWebServiceEndpointElement(pcName);
300: component.appendChild(endpointElement);
301: }
302:
303: Element binding = getElement(endpointElement,
304: MESSAGE_SECURITY_BINDING_TAG);
305:
306: if (binding == null) {
307: binding = createSecurityBindingElement();
308: endpointElement.appendChild(binding);
309: }
310:
311: binding.setAttribute(PROVIDER_ID_ATTR,
312: AM_SERVER_PROVIDER_PREFIX + providerId);
313:
314: writeDocument();
315: }
316:
317: public void setServiceRefMessageSecurityBinding(
318: String serviceRefName, String namespaceURI, String localPart) {
319: Element root = getRootElement();
320:
321: if (type == ProjectType.EJB) {
322: String ejbName = getEjbName(serviceRefName);
323: Element component = getComponentElement(root, EJB_TAG,
324: EJB_NAME_TAG, ejbName);
325: if (component == null) {
326: component = createEjbElement(ejbName);
327: root.appendChild(component);
328: }
329:
330: root = component;
331: }
332:
333: Element serviceRef = getServiceRefElement(root, serviceRefName,
334: namespaceURI, localPart);
335:
336: if (serviceRef == null) {
337: serviceRef = createServiceRefElement(serviceRefName,
338: namespaceURI, localPart);
339:
340: if (type == ProjectType.WEB) {
341: insertSunWebComponent(root, serviceRef);
342: } else {
343: root.appendChild(serviceRef);
344: }
345: }
346:
347: Element portInfo = getElement(serviceRef, PORT_INFO_TAG);
348:
349: Element binding = getElement(portInfo,
350: MESSAGE_SECURITY_BINDING_TAG);
351:
352: if (binding == null) {
353: binding = createSecurityBindingElement();
354: portInfo.appendChild(binding);
355: }
356:
357: binding.setAttribute(PROVIDER_ID_ATTR, AM_CLIENT_PROVIDER);
358:
359: writeDocument();
360: }
361:
362: public void removeServiceMessageSecurityBinding(String svcDescName,
363: String pcName) {
364: Element root = getRootElement();
365: Element component = null;
366: String componentTag = null;
367: String componentNameTag = null;
368:
369: if (type == ProjectType.WEB) {
370: componentTag = SERVLET_TAG;
371: componentNameTag = SERVLET_NAME_TAG;
372: } else if (type == ProjectType.EJB) {
373: componentTag = EJB_TAG;
374: componentNameTag = EJB_NAME_TAG;
375: }
376:
377: component = getComponentElement(root, componentTag,
378: componentNameTag, pcName);
379:
380: if (component != null) {
381: Element endpointElement = getElement(component,
382: WEBSERVICE_ENDPOINT_TAG);
383:
384: if (endpointElement != null) {
385: component.removeChild(endpointElement);
386: }
387: }
388:
389: writeDocument();
390: }
391:
392: public void removeServiceRefMessageSecurityBinding(
393: String serviceRefName, String namespaceURI, String localPart) {
394: Element root = getRootElement();
395:
396: if (type == ProjectType.EJB) {
397: String ejbName = getEjbName(serviceRefName);
398: root = getComponentElement(root, EJB_TAG, EJB_NAME_TAG,
399: ejbName);
400:
401: if (root == null)
402: return;
403: }
404:
405: Element serviceRef = getServiceRefElement(root, serviceRefName,
406: namespaceURI, localPart);
407:
408: if (serviceRef != null) {
409: root.removeChild(serviceRef);
410: }
411:
412: writeDocument();
413: }
414:
415: public boolean isServiceSecurityEnabled(String svcDescName,
416: String pcName) {
417: Element root = getRootElement();
418: Element component = null;
419: String componentTag = null;
420: String componentNameTag = null;
421:
422: if (type == ProjectType.WEB) {
423: componentTag = SERVLET_TAG;
424: componentNameTag = SERVLET_NAME_TAG;
425: } else if (type == ProjectType.EJB) {
426: componentTag = EJB_TAG;
427: componentNameTag = EJB_NAME_TAG;
428: } else {
429: return false;
430: }
431:
432: component = getComponentElement(root, componentTag,
433: componentNameTag, pcName);
434:
435: if (component != null) {
436: Element endpointElement = getElement(component,
437: WEBSERVICE_ENDPOINT_TAG);
438:
439: if (endpointElement != null) {
440: Element binding = getElement(endpointElement,
441: MESSAGE_SECURITY_BINDING_TAG);
442:
443: if (binding != null) {
444: if (binding.getAttribute(PROVIDER_ID_ATTR)
445: .startsWith(AM_SERVER_PROVIDER_PREFIX)) {
446: return true;
447: }
448: }
449: }
450: }
451:
452: return false;
453: }
454:
455: public boolean isClientSecurityEnabled(String serviceRefName,
456: String namespaceURI, String localPart) {
457: Element root = getRootElement();
458:
459: if (type == ProjectType.EJB) {
460: String ejbName = getEjbName(serviceRefName);
461: root = getComponentElement(root, EJB_TAG, EJB_NAME_TAG,
462: ejbName);
463:
464: if (root == null)
465: return false;
466: }
467:
468: Element serviceRef = getServiceRefElement(root, serviceRefName,
469: namespaceURI, localPart);
470:
471: if (serviceRef != null) {
472: Element portInfo = getElement(serviceRef, PORT_INFO_TAG);
473: Element binding = getElement(serviceRef,
474: MESSAGE_SECURITY_BINDING_TAG);
475:
476: if (binding != null) {
477: if (AM_CLIENT_PROVIDER.equals(binding
478: .getAttribute(PROVIDER_ID_ATTR))) {
479: return true;
480: }
481: }
482: }
483:
484: return false;
485: }
486:
487: private Element getRootElement() {
488: String tagName = null;
489:
490: if (type == ProjectType.WEB) {
491: tagName = SUN_WEB_APP_TAG;
492: } else if (type == ProjectType.EJB) {
493: tagName = ENTERPRISE_BEANS_TAG;
494: } else if (type == ProjectType.CLIENT) {
495: tagName = SUN_APPLICATION_CLIENT_TAG;
496: }
497:
498: return getElement(tagName);
499: }
500:
501: private Element getComponentElement(Element root,
502: String componentTag, String componentNameTag,
503: String componentName) {
504: NodeList components = root.getElementsByTagName(componentTag);
505: int length = components.getLength();
506:
507: for (int i = 0; i < length; i++) {
508: Element component = (Element) components.item(0);
509: NodeList names = component
510: .getElementsByTagName(componentNameTag);
511:
512: if (names.getLength() > 0) {
513: Element name = (Element) names.item(0);
514:
515: if (containsValue(name, componentName)) {
516: return component;
517: }
518: }
519: }
520:
521: return null;
522: }
523:
524: private Element getServiceRefElement(Element root,
525: String serviceRefName, String namespaceURI, String localPart) {
526: Document document = getDocument();
527: NodeList serviceRefs = document
528: .getElementsByTagName(SERVICE_REF_TAG);
529: int length = serviceRefs.getLength();
530:
531: for (int i = 0; i < length; i++) {
532: Element serviceRef = (Element) serviceRefs.item(i);
533: Element refName = getElement(serviceRef,
534: SERVICE_REF_NAME_TAG);
535:
536: if (refName != null) {
537: if (containsValue(refName, serviceRefName)) {
538: Element portInfo = getElement(serviceRef,
539: PORT_INFO_TAG);
540:
541: if (portInfo == null)
542: continue;
543:
544: Element wsdlPort = getElement(portInfo,
545: WSDL_PORT_TAG);
546:
547: if (wsdlPort == null)
548: continue;
549:
550: Element namespaceURIElement = getElement(wsdlPort,
551: NAMESPACE_URI_TAG);
552:
553: if (namespaceURIElement != null) {
554: if (containsValue(namespaceURIElement,
555: namespaceURI)) {
556: Element localPartElement = getElement(
557: wsdlPort, LOCALPART_TAG);
558:
559: if (localPartElement != null) {
560: if (containsValue(localPartElement,
561: localPart)) {
562: return serviceRef;
563: }
564: }
565: }
566: }
567: }
568: }
569: }
570:
571: return null;
572: }
573:
574: private Element createComponentElement(String componentTag,
575: String componentNameTag, String componentName) {
576: Document document = getDocument();
577: Element componentElement = document.createElement(componentTag);
578: componentElement.appendChild(createElement(componentNameTag,
579: componentName));
580:
581: return componentElement;
582: }
583:
584: private Element createServiceRefElement(String serviceRefName,
585: String namespaceURI, String localPart) {
586: Document document = getDocument();
587: Element serviceRef = document.createElement(SERVICE_REF_TAG);
588: serviceRef.appendChild(createElement(SERVICE_REF_NAME_TAG,
589: serviceRefName));
590: Element portInfo = document.createElement(PORT_INFO_TAG);
591: serviceRef.appendChild(portInfo);
592: Element wsdlPort = document.createElement(WSDL_PORT_TAG);
593: wsdlPort.appendChild(createElement(NAMESPACE_URI_TAG,
594: namespaceURI));
595: wsdlPort.appendChild(createElement(LOCALPART_TAG, localPart));
596: portInfo.appendChild(wsdlPort);
597:
598: return serviceRef;
599: }
600:
601: private Element getElement(String tagName) {
602: Document document = getDocument();
603:
604: NodeList elements = document.getElementsByTagName(tagName);
605:
606: if (elements.getLength() > 0) {
607: return (Element) elements.item(0);
608: }
609:
610: return null;
611: }
612:
613: private Element getElement(Element component, String tagName) {
614: NodeList elements = component.getElementsByTagName(tagName);
615:
616: if (elements.getLength() > 0) {
617: return (Element) elements.item(0);
618: }
619:
620: return null;
621: }
622:
623: private Element createEjbElement(String ejbName) {
624: Document document = getDocument();
625: Element ejb = document.createElement(EJB_TAG);
626: ejb.appendChild(createElement(EJB_NAME_TAG, ejbName));
627:
628: return ejb;
629: }
630:
631: private Element createWebServiceEndpointElement(String endpointName) {
632: Document document = getDocument();
633: Element endpoint = document
634: .createElement(WEBSERVICE_ENDPOINT_TAG);
635: endpoint.appendChild(createElement(PORT_COMPONENT_NAME_TAG,
636: endpointName));
637:
638: return endpoint;
639: }
640:
641: private Element createSecurityBindingElement() {
642: Document document = getDocument();
643: Element binding = document
644: .createElement(MESSAGE_SECURITY_BINDING_TAG);
645: binding.setAttribute(AUTH_LAYER_ATTR, AUTH_LAYER);
646: Element security = document.createElement(MESSAGE_SECURITY_TAG);
647: security.appendChild(document.createElement(MESSAGE_TAG));
648: Element request = document
649: .createElement(REQUEST_PROTECTION_TAG);
650: request.setAttribute(AUTH_SOURCE_ATTR,
651: REQUEST_POLICY_AUTH_SOURCE);
652: security.appendChild(request);
653: Element response = document
654: .createElement(RESPONSE_PROTECTION_TAG);
655: response.setAttribute(AUTH_SOURCE_ATTR,
656: RESPONSE_POLICY_AUTH_SOURCE);
657: security.appendChild(response);
658: binding.appendChild(security);
659:
660: return binding;
661: }
662:
663: private void insertSunWebComponent(Element root, Element component) {
664: Element classLoader = getElement(root, CLASS_LOADER_TAG);
665: root.insertBefore(component, classLoader);
666: }
667:
668: private Element getSecurityRoleMapping(String value) {
669: Document document = getDocument();
670: NodeList mappings = document
671: .getElementsByTagName(SECURITY_ROLE_MAPPING_TAG);
672: int length = mappings.getLength();
673:
674: for (int i = 0; i < length; i++) {
675: Element mapping = (Element) mappings.item(i);
676: NodeList roleNames = mapping
677: .getElementsByTagName(ROLE_NAME_TAG);
678:
679: if (roleNames.getLength() > 0) {
680: Element roleName = (Element) roleNames.item(0);
681:
682: if (containsValue(roleName, value)) {
683: return mapping;
684: }
685: }
686: }
687:
688: return null;
689: }
690:
691: private Element createSecurityRoleMapping(String value) {
692: Document document = getDocument();
693: Element mapping = document
694: .createElement(SECURITY_ROLE_MAPPING_TAG);
695: Element roleName = createElement(ROLE_NAME_TAG, value);
696: Element principal = createElement(PRINCIPAL_NAME_TAG, value);
697: mapping.appendChild(roleName);
698: mapping.appendChild(principal);
699:
700: return mapping;
701: }
702:
703: private Element createElement(String tag, String value) {
704: Document document = getDocument();
705: Element element = document.createElement(tag);
706: Text text = document.createTextNode(value);
707: element.appendChild(text);
708:
709: return element;
710: }
711:
712: private Element getPrincipalName(Element mapping, String value) {
713: NodeList nodes = mapping
714: .getElementsByTagName(PRINCIPAL_NAME_TAG);
715: int length = nodes.getLength();
716:
717: for (int i = 0; i < length; i++) {
718: Element principal = (Element) nodes.item(i);
719:
720: if (containsValue(principal, value)) {
721: return principal;
722: }
723: }
724:
725: return null;
726: }
727:
728: private String getEjbName(String refName) {
729: String ejbName = refName.substring(0, refName.indexOf("/")); //NOI18N
730: ejbName = ejbName.substring(ejbName.lastIndexOf(".") + 1); //NOI18N
731:
732: return ejbName;
733: }
734:
735: private boolean containsValue(Element element, String value) {
736: Node child = element.getFirstChild();
737:
738: if (child instanceof Text) {
739: return (((Text) child).getWholeText().equals(value));
740: }
741:
742: return false;
743: }
744:
745: private void writeDocument() {
746: beautify();
747:
748: //RequestProcessor.getDefault().post(new Runnable() {
749: // public void run() {
750: //FileLock lock = null;
751: OutputStream os = null;
752:
753: try {
754: Document document = getDocument();
755: DocumentType docType = document.getDoctype();
756: TransformerFactory factory = TransformerFactory
757: .newInstance();
758: Transformer transformer = factory.newTransformer();
759: DOMSource source = new DOMSource(document);
760:
761: //lock = sunDD.lock();
762: //os = sunDD.getOutputStream(lock);
763: os = new FileOutputStream(FileUtil.toFile(sunDD));
764:
765: StreamResult result = new StreamResult(os);
766:
767: transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
768: docType.getPublicId());
769: transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
770: docType.getSystemId());
771: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NOI18N
772: transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NOI18N
773: transformer.transform(source, result);
774:
775: //transformer.transform(source, new StreamResult(System.out));
776: } catch (Exception ex) {
777: ex.printStackTrace();
778: ErrorManager.getDefault().notify(
779: ErrorManager.INFORMATIONAL, ex);
780: } finally {
781: if (os != null) {
782: try {
783: os.close();
784: } catch (IOException ex) {
785: ErrorManager.getDefault().notify(
786: ErrorManager.INFORMATIONAL, ex);
787: }
788: }
789:
790: /*
791: if (lock != null) {
792: lock.releaseLock();
793: }
794: */
795: }
796: //}
797: //}, TIME_TO_WAIT);
798: }
799:
800: private Document getDocument() {
801: if (document == null) {
802: DocumentBuilder builder = getDocumentBuilder();
803: if (builder == null)
804: return null;
805:
806: //FileLock lock = null;
807: InputStream is = null;
808:
809: try {
810: //lock = sunDD.lock();
811: //is = sunDD.getInputStream();
812: is = new FileInputStream(FileUtil.toFile(sunDD));
813: document = builder.parse(is);
814: } catch (SAXException ex) {
815: ErrorManager.getDefault().notify(
816: ErrorManager.INFORMATIONAL, ex);
817: } catch (IOException ex) {
818: ErrorManager.getDefault().notify(
819: ErrorManager.INFORMATIONAL, ex);
820: } finally {
821: if (is != null) {
822: try {
823: is.close();
824: } catch (IOException ex) {
825: ErrorManager.getDefault().notify(
826: ErrorManager.INFORMATIONAL, ex);
827: }
828: }
829: /*
830: if (lock != null) {
831: lock.releaseLock();
832: }
833: */
834: }
835: }
836:
837: return document;
838: }
839:
840: private DocumentBuilder getDocumentBuilder() {
841: DocumentBuilder builder = null;
842:
843: DocumentBuilderFactory factory = DocumentBuilderFactory
844: .newInstance();
845: factory.setNamespaceAware(false);
846: factory.setIgnoringComments(false);
847: factory.setIgnoringElementContentWhitespace(true);
848: factory.setCoalescing(false);
849: factory.setExpandEntityReferences(false);
850: factory.setValidating(false);
851:
852: try {
853: builder = factory.newDocumentBuilder();
854: builder.setEntityResolver(new SunDTDResolver());
855: } catch (ParserConfigurationException ex) {
856: ErrorManager.getDefault().notify(
857: ErrorManager.INFORMATIONAL, ex);
858: }
859:
860: return builder;
861: }
862:
863: private void beautify() {
864: beautify(getDocument().getDocumentElement(), "\n"); //NOI18N
865: }
866:
867: private void beautify(Node node, String indent) {
868: Document document = getDocument();
869: NodeList children = node.getChildNodes();
870: int length = children.getLength();
871: ArrayList<Node> list = new ArrayList<Node>();
872:
873: for (int i = 0; i < length; i++) {
874: list.add(children.item(i));
875: }
876:
877: for (int i = 0; i < length; i++) {
878: Node child = list.get(i);
879:
880: if (child instanceof Text)
881: continue;
882:
883: node.insertBefore(document.createTextNode(indent + IDENT),
884: child);
885: beautify(child, indent + IDENT);
886:
887: if (i + 1 == length) {
888: node.appendChild(document.createTextNode(indent));
889: }
890: }
891: }
892:
893: /**
894: *
895: *
896: */
897: private static class SunDTDResolver implements EntityResolver {
898: public InputSource resolveEntity(String publicId,
899: String systemId) {
900: String dtd = null;
901:
902: if (SUN_WEB_SYSTEM_ID_2_4.equals(systemId)) {
903: dtd = SUN_WEB_DTD_2_4;
904: } else if (SUN_WEB_SYSTEM_ID_2_5.equals(systemId)) {
905: dtd = SUN_WEB_DTD_2_5;
906: } else if (SUN_EJB_SYSTEM_ID_3_0.equals(systemId)) {
907: dtd = SUN_EJB_DTD_3_0;
908: } else if (SUN_APPCLIENT_SYSTEM_ID_5_0.equals(systemId)) {
909: dtd = SUN_APPCLIENT_DTD_5_0;
910: }
911:
912: if (dtd != null) {
913: InputStream is = this .getClass().getResourceAsStream(
914: dtd);
915: InputStreamReader isr = new InputStreamReader(is);
916: return new InputSource(isr);
917: } else {
918: return null;
919: }
920: }
921: }
922: }
|