Source Code Cross Referenced for SunConversion.java in  » J2EE » openejb3 » org » apache » openejb » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */package org.apache.openejb.config;
0017:
0018:        import org.apache.openejb.jee.ApplicationClient;
0019:        import org.apache.openejb.jee.CmpField;
0020:        import org.apache.openejb.jee.EjbJar;
0021:        import org.apache.openejb.jee.EnterpriseBean;
0022:        import org.apache.openejb.jee.EntityBean;
0023:        import org.apache.openejb.jee.JndiReference;
0024:        import org.apache.openejb.jee.PersistenceType;
0025:        import org.apache.openejb.jee.PortComponent;
0026:        import org.apache.openejb.jee.PortComponentRef;
0027:        import org.apache.openejb.jee.ServiceImplBean;
0028:        import org.apache.openejb.jee.ServiceRef;
0029:        import org.apache.openejb.jee.WebApp;
0030:        import org.apache.openejb.jee.WebserviceDescription;
0031:        import org.apache.openejb.jee.jpa.AttributeOverride;
0032:        import org.apache.openejb.jee.jpa.Attributes;
0033:        import org.apache.openejb.jee.jpa.Basic;
0034:        import org.apache.openejb.jee.jpa.Column;
0035:        import org.apache.openejb.jee.jpa.Entity;
0036:        import org.apache.openejb.jee.jpa.EntityMappings;
0037:        import org.apache.openejb.jee.jpa.Field;
0038:        import org.apache.openejb.jee.jpa.Id;
0039:        import org.apache.openejb.jee.jpa.JoinColumn;
0040:        import org.apache.openejb.jee.jpa.JoinTable;
0041:        import org.apache.openejb.jee.jpa.ManyToOne;
0042:        import org.apache.openejb.jee.jpa.NamedQuery;
0043:        import org.apache.openejb.jee.jpa.OneToMany;
0044:        import org.apache.openejb.jee.jpa.OneToOne;
0045:        import org.apache.openejb.jee.jpa.PrimaryKeyJoinColumn;
0046:        import org.apache.openejb.jee.jpa.RelationField;
0047:        import org.apache.openejb.jee.jpa.SecondaryTable;
0048:        import org.apache.openejb.jee.jpa.Table;
0049:        import org.apache.openejb.jee.oejb3.EjbDeployment;
0050:        import org.apache.openejb.jee.oejb3.EjbLink;
0051:        import org.apache.openejb.jee.oejb3.OpenejbJar;
0052:        import org.apache.openejb.jee.oejb3.ResourceLink;
0053:        import org.apache.openejb.jee.sun.Cmp;
0054:        import org.apache.openejb.jee.sun.CmpFieldMapping;
0055:        import org.apache.openejb.jee.sun.CmrFieldMapping;
0056:        import org.apache.openejb.jee.sun.ColumnName;
0057:        import org.apache.openejb.jee.sun.ColumnPair;
0058:        import org.apache.openejb.jee.sun.Ejb;
0059:        import org.apache.openejb.jee.sun.EjbRef;
0060:        import org.apache.openejb.jee.sun.EntityMapping;
0061:        import org.apache.openejb.jee.sun.Finder;
0062:        import org.apache.openejb.jee.sun.JaxbSun;
0063:        import org.apache.openejb.jee.sun.MessageDestinationRef;
0064:        import org.apache.openejb.jee.sun.OneOneFinders;
0065:        import org.apache.openejb.jee.sun.PortInfo;
0066:        import org.apache.openejb.jee.sun.ResourceEnvRef;
0067:        import org.apache.openejb.jee.sun.ResourceRef;
0068:        import org.apache.openejb.jee.sun.StubProperty;
0069:        import org.apache.openejb.jee.sun.SunApplication;
0070:        import org.apache.openejb.jee.sun.SunApplicationClient;
0071:        import org.apache.openejb.jee.sun.SunCmpMapping;
0072:        import org.apache.openejb.jee.sun.SunCmpMappings;
0073:        import org.apache.openejb.jee.sun.SunEjbJar;
0074:        import org.apache.openejb.jee.sun.SunWebApp;
0075:        import org.apache.openejb.jee.sun.Web;
0076:        import org.apache.openejb.jee.sun.WebserviceEndpoint;
0077:        import org.apache.openejb.jee.sun.WsdlPort;
0078:
0079:        import javax.xml.namespace.QName;
0080:        import java.io.ByteArrayInputStream;
0081:        import java.net.URL;
0082:        import java.util.ArrayList;
0083:        import java.util.Arrays;
0084:        import java.util.Collection;
0085:        import java.util.Collections;
0086:        import java.util.HashMap;
0087:        import java.util.LinkedList;
0088:        import java.util.List;
0089:        import java.util.Map;
0090:        import java.util.StringTokenizer;
0091:        import java.util.TreeMap;
0092:
0093:        //
0094:        // Note to developer:  the best doc on what the sun-cmp-mappings element mean can be found here
0095:        //   http://java.sun.com/j2ee/1.4/docs/devguide/dgcmp.html
0096:        //   https://glassfish.dev.java.net/javaee5/docs/DG/beajj.html
0097:        //
0098:
0099:        public class SunConversion implements  DynamicDeployer {
0100:            public AppModule deploy(AppModule appModule) {
0101:                SunApplication sunApplication = getSunApplication(appModule);
0102:                if (sunApplication != null) {
0103:                    for (Web web : sunApplication.getWeb()) {
0104:                        String webUri = web.getWebUri();
0105:                        for (WebModule webModule : appModule.getWebModules()) {
0106:                            if (webUri.equals(webModule.getModuleId())) {
0107:                                webModule.setContextRoot(web.getContextRoot());
0108:                                break;
0109:                            }
0110:                        }
0111:                    }
0112:                }
0113:
0114:                for (EjbModule ejbModule : appModule.getEjbModules()) {
0115:                    convertModule(ejbModule, appModule.getCmpMappings());
0116:                }
0117:                for (ClientModule clientModule : appModule.getClientModules()) {
0118:                    convertModule(clientModule);
0119:                }
0120:                for (WebModule webModule : appModule.getWebModules()) {
0121:                    convertModule(webModule);
0122:                }
0123:                return appModule;
0124:            }
0125:
0126:            private SunApplication getSunApplication(AppModule appModule) {
0127:                Object altDD = appModule.getAltDDs().get("sun-application.xml");
0128:                if (altDD instanceof  String) {
0129:                    try {
0130:                        altDD = JaxbSun.unmarshal(SunApplication.class,
0131:                                new ByteArrayInputStream(((String) altDD)
0132:                                        .getBytes()));
0133:                    } catch (Exception e) {
0134:                        // todo warn about not being able to parse sun descriptor
0135:                    }
0136:                }
0137:                if (altDD instanceof  URL) {
0138:                    try {
0139:                        altDD = JaxbSun.unmarshal(SunApplication.class,
0140:                                ((URL) altDD).openStream());
0141:                    } catch (Exception e) {
0142:                        // todo warn about not being able to parse sun descriptor
0143:                    }
0144:                }
0145:                if (altDD instanceof  SunApplication) {
0146:                    return (SunApplication) altDD;
0147:                }
0148:                return null;
0149:            }
0150:
0151:            private SunApplicationClient getSunApplicationClient(
0152:                    ClientModule clientModule) {
0153:                Object altDD = clientModule.getAltDDs().get(
0154:                        "sun-application-client.xml");
0155:                if (altDD instanceof  String) {
0156:                    try {
0157:                        altDD = JaxbSun.unmarshal(SunApplicationClient.class,
0158:                                new ByteArrayInputStream(((String) altDD)
0159:                                        .getBytes()));
0160:                    } catch (Exception e) {
0161:                        // todo warn about not being able to parse sun descriptor
0162:                    }
0163:                }
0164:                if (altDD instanceof  URL) {
0165:                    try {
0166:                        altDD = JaxbSun.unmarshal(SunApplicationClient.class,
0167:                                ((URL) altDD).openStream());
0168:                    } catch (Exception e) {
0169:                        // todo warn about not being able to parse sun descriptor
0170:                    }
0171:                }
0172:                if (altDD instanceof  SunApplicationClient) {
0173:                    return (SunApplicationClient) altDD;
0174:                }
0175:                return null;
0176:            }
0177:
0178:            private SunWebApp getSunWebApp(WebModule webModule) {
0179:                Object altDD = webModule.getAltDDs().get("sun-web.xml");
0180:                if (altDD instanceof  String) {
0181:                    try {
0182:                        altDD = JaxbSun.unmarshal(SunWebApp.class,
0183:                                new ByteArrayInputStream(((String) altDD)
0184:                                        .getBytes()));
0185:                    } catch (Exception e) {
0186:                        // todo warn about not being able to parse sun descriptor
0187:                    }
0188:                }
0189:                if (altDD instanceof  URL) {
0190:                    try {
0191:                        altDD = JaxbSun.unmarshal(SunWebApp.class,
0192:                                ((URL) altDD).openStream());
0193:                    } catch (Exception e) {
0194:                        e.printStackTrace();
0195:                        // todo warn about not being able to parse sun descriptor
0196:                    }
0197:                }
0198:                if (altDD instanceof  SunWebApp) {
0199:                    return (SunWebApp) altDD;
0200:                }
0201:                return null;
0202:            }
0203:
0204:            private SunEjbJar getSunEjbJar(EjbModule ejbModule) {
0205:                Object altDD = ejbModule.getAltDDs().get("sun-ejb-jar.xml");
0206:                if (altDD instanceof  String) {
0207:                    try {
0208:                        altDD = JaxbSun.unmarshal(SunCmpMappings.class,
0209:                                new ByteArrayInputStream(((String) altDD)
0210:                                        .getBytes()));
0211:                    } catch (Exception e) {
0212:                        // todo warn about not being able to parse sun descriptor
0213:                    }
0214:                }
0215:                if (altDD instanceof  URL) {
0216:                    try {
0217:                        altDD = JaxbSun.unmarshal(SunEjbJar.class,
0218:                                ((URL) altDD).openStream());
0219:                    } catch (Exception e) {
0220:                        e.printStackTrace();
0221:                        // todo warn about not being able to parse sun descriptor
0222:                    }
0223:                }
0224:                if (altDD instanceof  SunEjbJar) {
0225:                    return (SunEjbJar) altDD;
0226:                }
0227:                return null;
0228:            }
0229:
0230:            private SunCmpMappings getSunCmpMappings(EjbModule ejbModule) {
0231:                Object altDD = ejbModule.getAltDDs()
0232:                        .get("sun-cmp-mappings.xml");
0233:                if (altDD instanceof  String) {
0234:                    try {
0235:                        altDD = JaxbSun.unmarshal(SunCmpMappings.class,
0236:                                new ByteArrayInputStream(((String) altDD)
0237:                                        .getBytes()));
0238:                    } catch (Exception e) {
0239:                        // todo warn about not being able to parse sun descriptor
0240:                    }
0241:                }
0242:                if (altDD instanceof  URL) {
0243:                    try {
0244:                        altDD = JaxbSun.unmarshal(SunCmpMappings.class,
0245:                                ((URL) altDD).openStream());
0246:                    } catch (Exception e) {
0247:                        e.printStackTrace();
0248:                        // todo warn about not being able to parse sun descriptor
0249:                    }
0250:                }
0251:                if (altDD instanceof  SunCmpMappings) {
0252:                    return (SunCmpMappings) altDD;
0253:                }
0254:                return null;
0255:            }
0256:
0257:            public void convertModule(ClientModule clientModule) {
0258:                if (clientModule == null) {
0259:                    return;
0260:                }
0261:
0262:                ApplicationClient applicationClient = clientModule
0263:                        .getApplicationClient();
0264:                if (applicationClient == null) {
0265:                    return;
0266:                }
0267:                SunApplicationClient sunApplicationClient = getSunApplicationClient(clientModule);
0268:                if (sunApplicationClient == null) {
0269:                    return;
0270:                }
0271:
0272:                // map ejb-refs
0273:                Map<String, org.apache.openejb.jee.EjbRef> refMap = applicationClient
0274:                        .getEjbRefMap();
0275:
0276:                // map ejb-ref jndi name declaration to deploymentId
0277:                for (EjbRef ref : sunApplicationClient.getEjbRef()) {
0278:                    if (ref.getJndiName() != null) {
0279:                        String refName = ref.getEjbRefName();
0280:                        org.apache.openejb.jee.EjbRef ejbRef = refMap
0281:                                .get(refName);
0282:                        if (ejbRef == null) {
0283:                            ejbRef = new org.apache.openejb.jee.EjbRef();
0284:                            ejbRef.setEjbRefName(refName);
0285:                            refMap.put(refName, ejbRef);
0286:                            applicationClient.getEjbRef().add(ejbRef);
0287:                        }
0288:                        ejbRef.setMappedName(ref.getJndiName());
0289:                    }
0290:                }
0291:
0292:                // map resource-env-refs and message-destination-refs
0293:                Map<String, JndiReference> resEnvMap = new TreeMap<String, JndiReference>();
0294:                resEnvMap.putAll(applicationClient.getResourceEnvRefMap());
0295:                resEnvMap.putAll(applicationClient
0296:                        .getMessageDestinationRefMap());
0297:
0298:                for (ResourceRef ref : sunApplicationClient.getResourceRef()) {
0299:                    if (ref.getJndiName() != null) {
0300:                        String refName = ref.getResRefName();
0301:                        JndiReference resEnvRef = resEnvMap.get(refName);
0302:                        if (resEnvRef != null) {
0303:                            resEnvRef.setMappedName(ref.getJndiName());
0304:                        }
0305:                    }
0306:                }
0307:                for (ResourceEnvRef ref : sunApplicationClient
0308:                        .getResourceEnvRef()) {
0309:                    if (ref.getJndiName() != null) {
0310:                        String refName = ref.getResourceEnvRefName();
0311:                        JndiReference resEnvRef = resEnvMap.get(refName);
0312:                        if (resEnvRef != null) {
0313:                            resEnvRef.setMappedName(ref.getJndiName());
0314:                        }
0315:                    }
0316:                }
0317:                for (MessageDestinationRef ref : sunApplicationClient
0318:                        .getMessageDestinationRef()) {
0319:                    if (ref.getJndiName() != null) {
0320:                        String refName = ref.getMessageDestinationRefName();
0321:                        JndiReference resEnvRef = resEnvMap.get(refName);
0322:                        if (resEnvRef != null) {
0323:                            resEnvRef.setMappedName(ref.getJndiName());
0324:                        }
0325:                    }
0326:                }
0327:
0328:                Map<String, ServiceRef> serviceRefMap = applicationClient
0329:                        .getServiceRefMap();
0330:                for (org.apache.openejb.jee.sun.ServiceRef ref : sunApplicationClient
0331:                        .getServiceRef()) {
0332:                    String refName = ref.getServiceRefName();
0333:                    ServiceRef serviceRef = serviceRefMap.get(refName);
0334:                    if (serviceRef != null) {
0335:                        Map<String, PortComponentRef> ports = new TreeMap<String, PortComponentRef>();
0336:                        for (PortComponentRef portComponentRef : serviceRef
0337:                                .getPortComponentRef()) {
0338:                            ports.put(portComponentRef
0339:                                    .getServiceEndpointInterface(),
0340:                                    portComponentRef);
0341:                        }
0342:
0343:                        for (PortInfo portInfo : ref.getPortInfo()) {
0344:                            PortComponentRef portComponentRef = ports
0345:                                    .get(portInfo.getServiceEndpointInterface());
0346:                            if (portComponentRef != null) {
0347:                                WsdlPort wsdlPort = portInfo.getWsdlPort();
0348:                                if (wsdlPort != null) {
0349:                                    QName qname = new QName(wsdlPort
0350:                                            .getNamespaceURI(), wsdlPort
0351:                                            .getLocalpart());
0352:                                    portComponentRef.setQName(qname);
0353:                                }
0354:                                for (StubProperty stubProperty : portInfo
0355:                                        .getStubProperty()) {
0356:                                    String name = stubProperty.getName();
0357:                                    String value = stubProperty.getValue();
0358:                                    portComponentRef.getProperties()
0359:                                            .setProperty(name, value);
0360:                                }
0361:                            }
0362:                        }
0363:
0364:                        String wsdlOverride = ref.getWsdlOverride();
0365:                        if (wsdlOverride != null && wsdlOverride.length() > 0) {
0366:                            String serviceId = extractServiceId(wsdlOverride);
0367:                            serviceRef.setMappedName(serviceId);
0368:                        }
0369:                    }
0370:                }
0371:            }
0372:
0373:            public void convertModule(WebModule webModule) {
0374:                if (webModule == null) {
0375:                    return;
0376:                }
0377:
0378:                WebApp webApp = webModule.getWebApp();
0379:                if (webApp == null) {
0380:                    return;
0381:                }
0382:                SunWebApp sunWebApp = getSunWebApp(webModule);
0383:                if (sunWebApp == null) {
0384:                    return;
0385:                }
0386:
0387:                if (sunWebApp.getContextRoot() != null) {
0388:                    webModule.setContextRoot(sunWebApp.getContextRoot());
0389:                }
0390:
0391:                // map ejb-refs
0392:                Map<String, org.apache.openejb.jee.JndiReference> refMap = new TreeMap<String, org.apache.openejb.jee.JndiReference>();
0393:                refMap.putAll(webApp.getEjbRefMap());
0394:                refMap.putAll(webApp.getEjbLocalRefMap());
0395:
0396:                // map ejb-ref jndi name declaration to deploymentId
0397:                for (EjbRef ref : sunWebApp.getEjbRef()) {
0398:                    if (ref.getJndiName() != null) {
0399:                        String refName = ref.getEjbRefName();
0400:                        org.apache.openejb.jee.JndiReference ejbRef = refMap
0401:                                .get(refName);
0402:                        if (ejbRef == null) {
0403:                            ejbRef = new org.apache.openejb.jee.EjbRef();
0404:                            ejbRef.setName(refName);
0405:                            refMap.put(refName, ejbRef);
0406:                            webApp.getEjbRef().add(
0407:                                    (org.apache.openejb.jee.EjbRef) ejbRef);
0408:                        }
0409:                        ejbRef.setMappedName(ref.getJndiName());
0410:                    }
0411:                }
0412:
0413:                // map resource-env-refs and message-destination-refs
0414:                Map<String, JndiReference> resEnvMap = new TreeMap<String, JndiReference>();
0415:                resEnvMap.putAll(webApp.getResourceRefMap());
0416:                resEnvMap.putAll(webApp.getResourceEnvRefMap());
0417:                resEnvMap.putAll(webApp.getMessageDestinationRefMap());
0418:
0419:                for (ResourceRef ref : sunWebApp.getResourceRef()) {
0420:                    if (ref.getJndiName() != null) {
0421:                        String refName = ref.getResRefName();
0422:                        JndiReference resEnvRef = resEnvMap.get(refName);
0423:                        if (resEnvRef != null) {
0424:                            resEnvRef.setMappedName(ref.getJndiName());
0425:                        }
0426:                    }
0427:                }
0428:                for (ResourceEnvRef ref : sunWebApp.getResourceEnvRef()) {
0429:                    if (ref.getJndiName() != null) {
0430:                        String refName = ref.getResourceEnvRefName();
0431:                        JndiReference resEnvRef = resEnvMap.get(refName);
0432:                        if (resEnvRef != null) {
0433:                            resEnvRef.setMappedName(ref.getJndiName());
0434:                        }
0435:                    }
0436:                }
0437:                for (MessageDestinationRef ref : sunWebApp
0438:                        .getMessageDestinationRef()) {
0439:                    if (ref.getJndiName() != null) {
0440:                        String refName = ref.getMessageDestinationRefName();
0441:                        JndiReference resEnvRef = resEnvMap.get(refName);
0442:                        if (resEnvRef != null) {
0443:                            resEnvRef.setMappedName(ref.getJndiName());
0444:                        }
0445:                    }
0446:                }
0447:
0448:                Map<String, ServiceRef> serviceRefMap = webApp
0449:                        .getServiceRefMap();
0450:                for (org.apache.openejb.jee.sun.ServiceRef ref : sunWebApp
0451:                        .getServiceRef()) {
0452:                    String refName = ref.getServiceRefName();
0453:                    ServiceRef serviceRef = serviceRefMap.get(refName);
0454:                    if (serviceRef != null) {
0455:                        Map<String, PortComponentRef> ports = new TreeMap<String, PortComponentRef>();
0456:                        for (PortComponentRef portComponentRef : serviceRef
0457:                                .getPortComponentRef()) {
0458:                            ports.put(portComponentRef
0459:                                    .getServiceEndpointInterface(),
0460:                                    portComponentRef);
0461:                        }
0462:
0463:                        for (PortInfo portInfo : ref.getPortInfo()) {
0464:                            PortComponentRef portComponentRef = ports
0465:                                    .get(portInfo.getServiceEndpointInterface());
0466:                            if (portComponentRef != null) {
0467:                                WsdlPort wsdlPort = portInfo.getWsdlPort();
0468:                                if (wsdlPort != null) {
0469:                                    QName qname = new QName(wsdlPort
0470:                                            .getNamespaceURI(), wsdlPort
0471:                                            .getLocalpart());
0472:                                    portComponentRef.setQName(qname);
0473:                                }
0474:                                for (StubProperty stubProperty : portInfo
0475:                                        .getStubProperty()) {
0476:                                    String name = stubProperty.getName();
0477:                                    String value = stubProperty.getValue();
0478:                                    portComponentRef.getProperties()
0479:                                            .setProperty(name, value);
0480:                                }
0481:                            }
0482:                        }
0483:
0484:                        String wsdlOverride = ref.getWsdlOverride();
0485:                        if (wsdlOverride != null && wsdlOverride.length() > 0) {
0486:                            String serviceId = extractServiceId(wsdlOverride);
0487:                            serviceRef.setMappedName(serviceId);
0488:                        }
0489:                    }
0490:                }
0491:
0492:                // map wsdl locations
0493:                if (webModule.getWebservices() != null) {
0494:                    Map<String, WebserviceDescription> descriptions = webModule
0495:                            .getWebservices().getWebserviceDescriptionMap();
0496:                    for (org.apache.openejb.jee.sun.WebserviceDescription sunDescription : sunWebApp
0497:                            .getWebserviceDescription()) {
0498:                        WebserviceDescription description = descriptions
0499:                                .get(sunDescription
0500:                                        .getWebserviceDescriptionName());
0501:                        if (description == null)
0502:                            continue;
0503:
0504:                        String serviceId = extractSerivceId(sunDescription
0505:                                .getWsdlPublishLocation(), description
0506:                                .getWsdlFile());
0507:                        if (serviceId != null) {
0508:                            description.setId(serviceId);
0509:                        }
0510:                    }
0511:                }
0512:            }
0513:
0514:            public static String extractServiceId(String location) {
0515:                return extractSerivceId(location, null);
0516:            }
0517:
0518:            public static String extractSerivceId(String location,
0519:                    String wsdlFile) {
0520:                if (location == null)
0521:                    return null;
0522:
0523:                if (location.startsWith("file:")) {
0524:                    // location format = file:{repository}/{location}.wsdl
0525:                    location = location.replaceFirst("file:[^/]*/", "");
0526:
0527:                    // append wsdl name without leading META-INF/wsdl or WEB-INF/wsdl or ending .wsdl
0528:                    if (wsdlFile != null) {
0529:                        wsdlFile = wsdlFile.replaceFirst("META-INF/wsdl/", "");
0530:                        wsdlFile = wsdlFile.replaceFirst("WEB-INF/wsdl/", "");
0531:                        location = location + "/" + wsdlFile;
0532:                    }
0533:                    location = location.replaceFirst("\\.wsdl$", "");
0534:                } else if (location.startsWith("http:")
0535:                        || location.startsWith("https:")) {
0536:                    // location format = https://{server}:{port}/{location}?WSDL
0537:                    location = location.replaceFirst("http[s]?://[^/]*/", "");
0538:                    location = location.replaceFirst("\\?.*$", "");
0539:                }
0540:
0541:                if (location.length() == 0)
0542:                    location = null;
0543:                return location;
0544:            }
0545:
0546:            public void convertModule(EjbModule ejbModule,
0547:                    EntityMappings entityMappings) {
0548:                Map<String, EntityData> entities = new TreeMap<String, EntityData>();
0549:                if (entityMappings != null)
0550:                    for (Entity entity : entityMappings.getEntity()) {
0551:                        entities.put(entity.getDescription(),
0552:                                new SunConversion.EntityData(entity));
0553:                    }
0554:
0555:                // merge data from sun-ejb-jar.xml file
0556:                SunEjbJar sunEjbJar = getSunEjbJar(ejbModule);
0557:                mergeEjbConfig(ejbModule, sunEjbJar);
0558:                mergeEntityMappings(entities, ejbModule.getModuleId(),
0559:                        ejbModule.getEjbJar(), ejbModule.getOpenejbJar(),
0560:                        sunEjbJar);
0561:
0562:                // merge data from sun-cmp-mappings.xml file
0563:                SunCmpMappings sunCmpMappings = getSunCmpMappings(ejbModule);
0564:                if (sunCmpMappings != null) {
0565:                    for (SunCmpMapping sunCmpMapping : sunCmpMappings
0566:                            .getSunCmpMapping()) {
0567:                        mergeEntityMappings(entities, ejbModule.getModuleId(),
0568:                                ejbModule, entityMappings, sunCmpMapping);
0569:                    }
0570:                }
0571:            }
0572:
0573:            private void mergeEjbConfig(EjbModule ejbModule, SunEjbJar sunEjbJar) {
0574:                EjbJar ejbJar = ejbModule.getEjbJar();
0575:                OpenejbJar openejbJar = ejbModule.getOpenejbJar();
0576:
0577:                if (openejbJar == null)
0578:                    return;
0579:                if (sunEjbJar == null)
0580:                    return;
0581:                if (sunEjbJar.getEnterpriseBeans() == null)
0582:                    return;
0583:
0584:                Map<String, Map<String, WebserviceEndpoint>> endpointMap = new HashMap<String, Map<String, WebserviceEndpoint>>();
0585:                for (Ejb ejb : sunEjbJar.getEnterpriseBeans().getEjb()) {
0586:                    EjbDeployment deployment = openejbJar
0587:                            .getDeploymentsByEjbName().get(ejb.getEjbName());
0588:                    if (deployment == null) {
0589:                        // warn no matching deployment
0590:                        continue;
0591:                    }
0592:
0593:                    // ejb jndi name is the deploymentId
0594:                    if (ejb.getJndiName() != null) {
0595:                        deployment.setDeploymentId(ejb.getJndiName());
0596:                    }
0597:
0598:                    // map ejb-ref jndi name declaration to deploymentId
0599:                    Map<String, EjbLink> linksMap = deployment.getEjbLinksMap();
0600:                    for (EjbRef ref : ejb.getEjbRef()) {
0601:                        if (ref.getJndiName() != null) {
0602:                            String refName = ref.getEjbRefName();
0603:                            EjbLink link = linksMap.get(refName);
0604:                            if (link == null) {
0605:                                link = new EjbLink();
0606:                                link.setEjbRefName(refName);
0607:                                linksMap.put(refName, link);
0608:                                deployment.getEjbLink().add(link);
0609:                            }
0610:                            link.setDeployentId(ref.getJndiName());
0611:                        }
0612:                    }
0613:
0614:                    Map<String, ResourceLink> resourceLinksMap = deployment
0615:                            .getResourceLinksMap();
0616:                    for (ResourceRef ref : ejb.getResourceRef()) {
0617:                        if (ref.getJndiName() != null) {
0618:                            String refName = ref.getResRefName();
0619:                            ResourceLink link = resourceLinksMap.get(refName);
0620:                            if (link == null) {
0621:                                link = new ResourceLink();
0622:                                link.setResRefName(refName);
0623:                                resourceLinksMap.put(refName, link);
0624:                                deployment.getResourceLink().add(link);
0625:                            }
0626:                            link.setResId(ref.getJndiName());
0627:                        }
0628:                    }
0629:
0630:                    for (ResourceEnvRef ref : ejb.getResourceEnvRef()) {
0631:                        if (ref.getJndiName() != null) {
0632:                            String refName = ref.getResourceEnvRefName();
0633:                            ResourceLink link = resourceLinksMap.get(refName);
0634:                            if (link == null) {
0635:                                link = new ResourceLink();
0636:                                link.setResRefName(refName);
0637:                                resourceLinksMap.put(refName, link);
0638:                                deployment.getResourceLink().add(link);
0639:                            }
0640:                            link.setResId(ref.getJndiName());
0641:                        }
0642:                    }
0643:
0644:                    for (MessageDestinationRef ref : ejb
0645:                            .getMessageDestinationRef()) {
0646:                        if (ref.getJndiName() != null) {
0647:                            String refName = ref.getMessageDestinationRefName();
0648:                            ResourceLink link = resourceLinksMap.get(refName);
0649:                            if (link == null) {
0650:                                link = new ResourceLink();
0651:                                link.setResRefName(refName);
0652:                                resourceLinksMap.put(refName, link);
0653:                                deployment.getResourceLink().add(link);
0654:                            }
0655:                            link.setResId(ref.getJndiName());
0656:                        }
0657:                    }
0658:
0659:                    EnterpriseBean bean = ejbJar.getEnterpriseBeansByEjbName()
0660:                            .get(ejb.getEjbName());
0661:                    if (bean != null) {
0662:                        Map<String, ServiceRef> serviceRefMap = bean
0663:                                .getServiceRefMap();
0664:                        for (org.apache.openejb.jee.sun.ServiceRef ref : ejb
0665:                                .getServiceRef()) {
0666:                            String refName = ref.getServiceRefName();
0667:                            ServiceRef serviceRef = serviceRefMap.get(refName);
0668:                            if (serviceRef != null) {
0669:                                Map<String, PortComponentRef> ports = new TreeMap<String, PortComponentRef>();
0670:                                for (PortComponentRef portComponentRef : serviceRef
0671:                                        .getPortComponentRef()) {
0672:                                    ports.put(portComponentRef
0673:                                            .getServiceEndpointInterface(),
0674:                                            portComponentRef);
0675:                                }
0676:
0677:                                for (PortInfo portInfo : ref.getPortInfo()) {
0678:                                    PortComponentRef portComponentRef = ports
0679:                                            .get(portInfo
0680:                                                    .getServiceEndpointInterface());
0681:                                    if (portComponentRef != null) {
0682:                                        WsdlPort wsdlPort = portInfo
0683:                                                .getWsdlPort();
0684:                                        if (wsdlPort != null) {
0685:                                            QName qname = new QName(wsdlPort
0686:                                                    .getNamespaceURI(),
0687:                                                    wsdlPort.getLocalpart());
0688:                                            portComponentRef.setQName(qname);
0689:                                        }
0690:                                        for (StubProperty stubProperty : portInfo
0691:                                                .getStubProperty()) {
0692:                                            String name = stubProperty
0693:                                                    .getName();
0694:                                            String value = stubProperty
0695:                                                    .getValue();
0696:                                            portComponentRef.getProperties()
0697:                                                    .setProperty(name, value);
0698:                                        }
0699:                                    }
0700:                                }
0701:
0702:                                String wsdlOverride = ref.getWsdlOverride();
0703:                                if (wsdlOverride != null
0704:                                        && wsdlOverride.length() > 0) {
0705:                                    String serviceId = extractServiceId(wsdlOverride);
0706:                                    serviceRef.setMappedName(serviceId);
0707:                                }
0708:                            }
0709:                        }
0710:                    }
0711:
0712:                    if (ejb.getMdbResourceAdapter() != null) {
0713:                        // resource adapter id is the MDB container ID
0714:                        String resourceAdapterId = ejb.getMdbResourceAdapter()
0715:                                .getResourceAdapterMid();
0716:                        deployment.setContainerId(resourceAdapterId);
0717:                    }
0718:
0719:                    endpointMap.put(ejb.getEjbName(), ejb
0720:                            .getWebserviceEndpointMap());
0721:                }
0722:
0723:                // map wsdl locations
0724:                if (ejbModule.getWebservices() != null) {
0725:                    Map<String, org.apache.openejb.jee.sun.WebserviceDescription> sunDescriptions = sunEjbJar
0726:                            .getEnterpriseBeans().getWebserviceDescriptionMap();
0727:                    for (WebserviceDescription description : ejbModule
0728:                            .getWebservices().getWebserviceDescription()) {
0729:                        org.apache.openejb.jee.sun.WebserviceDescription sunDescription = sunDescriptions
0730:                                .get(description.getWebserviceDescriptionName());
0731:
0732:                        // get the serviceId if specified
0733:                        String serviceId = null;
0734:                        if (sunDescription != null) {
0735:                            serviceId = extractSerivceId(sunDescription
0736:                                    .getWsdlPublishLocation(), description
0737:                                    .getWsdlFile());
0738:                        }
0739:                        if (serviceId != null) {
0740:                            description.setId(serviceId);
0741:                        }
0742:
0743:                        for (PortComponent port : description
0744:                                .getPortComponent()) {
0745:                            // set the ejb bind location
0746:                            ServiceImplBean bean = port.getServiceImplBean();
0747:                            if (bean != null && bean.getEjbLink() != null) {
0748:                                Map<String, WebserviceEndpoint> endpoints = endpointMap
0749:                                        .get(bean.getEjbLink());
0750:                                if (endpoints != null) {
0751:                                    WebserviceEndpoint endpoint = endpoints
0752:                                            .get(port.getPortComponentName());
0753:                                    if (endpoint != null
0754:                                            && endpoint.getEndpointAddressUri() != null) {
0755:                                        port.setLocation(endpoint
0756:                                                .getEndpointAddressUri());
0757:                                    }
0758:                                }
0759:                            }
0760:                        }
0761:                    }
0762:                }
0763:            }
0764:
0765:            private void mergeEntityMappings(Map<String, EntityData> entities,
0766:                    String moduleId, EjbJar ejbJar, OpenejbJar openejbJar,
0767:                    SunEjbJar sunEjbJar) {
0768:                if (openejbJar == null)
0769:                    return;
0770:                if (sunEjbJar == null)
0771:                    return;
0772:                if (sunEjbJar.getEnterpriseBeans() == null)
0773:                    return;
0774:
0775:                for (Ejb ejb : sunEjbJar.getEnterpriseBeans().getEjb()) {
0776:                    Cmp cmp = ejb.getCmp();
0777:                    if (cmp == null) {
0778:                        // skip non cmp beans
0779:                        continue;
0780:                    }
0781:
0782:                    // skip all non-CMP beans
0783:                    EnterpriseBean enterpriseBean = ejbJar
0784:                            .getEnterpriseBean(ejb.getEjbName());
0785:                    if (!(enterpriseBean instanceof  org.apache.openejb.jee.EntityBean)
0786:                            || ((EntityBean) enterpriseBean)
0787:                                    .getPersistenceType() != PersistenceType.CONTAINER) {
0788:                        continue;
0789:                    }
0790:                    EntityBean bean = (EntityBean) enterpriseBean;
0791:                    EntityData entityData = entities.get(moduleId + "#"
0792:                            + ejb.getEjbName());
0793:                    if (entityData == null) {
0794:                        // todo warn no such ejb in the ejb-jar.xml
0795:                        continue;
0796:                    }
0797:
0798:                    Collection<String> cmpFields = new ArrayList<String>(bean
0799:                            .getCmpField().size());
0800:                    for (CmpField cmpField : bean.getCmpField()) {
0801:                        cmpFields.add(cmpField.getFieldName());
0802:                    }
0803:
0804:                    OneOneFinders oneOneFinders = cmp.getOneOneFinders();
0805:                    if (oneOneFinders != null) {
0806:                        for (Finder finder : oneOneFinders.getFinder()) {
0807:                            List<List<String>> params = parseQueryParamters(finder
0808:                                    .getQueryParams());
0809:                            String queryFilter = finder.getQueryFilter();
0810:                            String ejbQl = convertToEjbQl(entityData.entity
0811:                                    .getName(), cmpFields, finder
0812:                                    .getQueryParams(), queryFilter);
0813:
0814:                            NamedQuery namedQuery = new NamedQuery();
0815:
0816:                            StringBuilder name = new StringBuilder();
0817:                            name.append(entityData.entity.getName())
0818:                                    .append(".").append(finder.getMethodName());
0819:                            if (!params.isEmpty()) {
0820:                                name.append('(');
0821:                                boolean first = true;
0822:                                for (List<String> methodParam : params) {
0823:                                    if (!first)
0824:                                        name.append(",");
0825:                                    name.append(methodParam.get(0));
0826:                                    first = false;
0827:                                }
0828:                                name.append(')');
0829:                            }
0830:                            namedQuery.setName(name.toString());
0831:                            namedQuery.setQuery(ejbQl);
0832:                            entityData.entity.getNamedQuery().add(namedQuery);
0833:                        }
0834:                    }
0835:                }
0836:            }
0837:
0838:            public void mergeEntityMappings(Map<String, EntityData> entities,
0839:                    String moduleId, EjbModule ejbModule,
0840:                    EntityMappings entityMappings, SunCmpMapping sunCmpMapping) {
0841:                for (EntityMapping bean : sunCmpMapping.getEntityMapping()) {
0842:                    SunConversion.EntityData entityData = entities.get(moduleId
0843:                            + "#" + bean.getEjbName());
0844:                    if (entityData == null) {
0845:                        // todo warn no such ejb in the ejb-jar.xml
0846:                        continue;
0847:                    }
0848:
0849:                    Table table = new Table();
0850:                    // table.setSchema(schema);
0851:                    table.setName(bean.getTableName());
0852:                    entityData.entity.setTable(table);
0853:
0854:                    // warn about no equivalent of the consistence modes in sun file
0855:
0856:                    for (org.apache.openejb.jee.sun.SecondaryTable sunSecondaryTable : bean
0857:                            .getSecondaryTable()) {
0858:                        SecondaryTable secondaryTable = new SecondaryTable();
0859:                        secondaryTable
0860:                                .setName(sunSecondaryTable.getTableName());
0861:                        for (ColumnPair columnPair : sunSecondaryTable
0862:                                .getColumnPair()) {
0863:                            SunColumnName localColumnName = new SunColumnName(
0864:                                    columnPair.getColumnName().get(0), table
0865:                                            .getName());
0866:                            SunColumnName referencedColumnName = new SunColumnName(
0867:                                    columnPair.getColumnName().get(1), table
0868:                                            .getName());
0869:
0870:                            // if user specified in reverse order, swap
0871:                            if (localColumnName.table != null) {
0872:                                SunColumnName temp = localColumnName;
0873:                                localColumnName = referencedColumnName;
0874:                                referencedColumnName = temp;
0875:                            }
0876:
0877:                            PrimaryKeyJoinColumn primaryKeyJoinColumn = new PrimaryKeyJoinColumn();
0878:                            primaryKeyJoinColumn
0879:                                    .setName(localColumnName.column);
0880:                            primaryKeyJoinColumn
0881:                                    .setReferencedColumnName(referencedColumnName.column);
0882:                            secondaryTable.getPrimaryKeyJoinColumn().add(
0883:                                    primaryKeyJoinColumn);
0884:                        }
0885:                    }
0886:
0887:                    for (CmpFieldMapping cmpFieldMapping : bean
0888:                            .getCmpFieldMapping()) {
0889:                        String fieldName = cmpFieldMapping.getFieldName();
0890:                        Field field = entityData.fields.get(fieldName);
0891:
0892:                        if (field == null) {
0893:                            // todo warn no such cmp-field in the ejb-jar.xml
0894:                            continue;
0895:                        }
0896:
0897:                        boolean readOnly = cmpFieldMapping.getReadOnly() != null;
0898:
0899:                        for (ColumnName columnName : cmpFieldMapping
0900:                                .getColumnName()) {
0901:                            SunColumnName sunColumnName = new SunColumnName(
0902:                                    columnName, table.getName());
0903:                            Column column = new Column();
0904:                            column.setTable(sunColumnName.table);
0905:                            column.setName(sunColumnName.column);
0906:                            if (readOnly) {
0907:                                column.setInsertable(false);
0908:                                column.setUpdatable(false);
0909:                            }
0910:                            field.setColumn(column);
0911:                        }
0912:                        // todo set fetch lazy when fetchWith is null
0913:                        // FetchedWith fetchedWith = cmpFieldMapping.getFetchedWith();
0914:                    }
0915:
0916:                    for (CmrFieldMapping cmrFieldMapping : bean
0917:                            .getCmrFieldMapping()) {
0918:                        String fieldName = cmrFieldMapping.getCmrFieldName();
0919:                        cmrFieldMapping.getColumnPair();
0920:                        RelationField field = entityData.relations
0921:                                .get(fieldName);
0922:                        if (field == null) {
0923:                            // todo warn no such cmr-field in the ejb-jar.xml
0924:                            continue;
0925:                        }
0926:
0927:                        if (field instanceof  OneToOne) {
0928:                            for (ColumnPair columnPair : cmrFieldMapping
0929:                                    .getColumnPair()) {
0930:                                SunColumnName localColumnName = new SunColumnName(
0931:                                        columnPair.getColumnName().get(0),
0932:                                        table.getName());
0933:                                SunColumnName referencedColumnName = new SunColumnName(
0934:                                        columnPair.getColumnName().get(1),
0935:                                        table.getName());
0936:
0937:                                // if user specified in reverse order, swap
0938:                                if (localColumnName.table != null) {
0939:                                    SunColumnName temp = localColumnName;
0940:                                    localColumnName = referencedColumnName;
0941:                                    referencedColumnName = temp;
0942:                                }
0943:
0944:                                boolean isFk = !entityData
0945:                                        .hasPkColumnMapping(localColumnName.column);
0946:                                if (isFk) {
0947:                                    // Make sure that the field with the FK is marked as the owning field
0948:                                    field.setMappedBy(null);
0949:                                    field.getRelatedField().setMappedBy(
0950:                                            field.getName());
0951:
0952:                                    JoinColumn joinColumn = new JoinColumn();
0953:                                    joinColumn.setName(localColumnName.column);
0954:                                    joinColumn
0955:                                            .setReferencedColumnName(referencedColumnName.column);
0956:                                    field.getJoinColumn().add(joinColumn);
0957:                                }
0958:                            }
0959:                        } else if (field instanceof  OneToMany) {
0960:                            // Bi-directional OneToMany do not have field mappings
0961:                            if (!field.getRelatedField().isSyntheticField()) {
0962:                                continue;
0963:                            }
0964:
0965:                            for (ColumnPair columnPair : cmrFieldMapping
0966:                                    .getColumnPair()) {
0967:                                SunColumnName localColumnName = new SunColumnName(
0968:                                        columnPair.getColumnName().get(0),
0969:                                        table.getName());
0970:                                SunColumnName otherColumnName = new SunColumnName(
0971:                                        columnPair.getColumnName().get(1),
0972:                                        table.getName());
0973:
0974:                                // if user specified in reverse order, swap
0975:                                if (localColumnName.table != null) {
0976:                                    SunColumnName temp = localColumnName;
0977:                                    localColumnName = otherColumnName;
0978:                                    otherColumnName = temp;
0979:                                }
0980:
0981:                                JoinColumn joinColumn = new JoinColumn();
0982:                                // for OneToMany the join column name is the other (fk) column
0983:                                joinColumn.setName(otherColumnName.column);
0984:                                // and the referenced column is the local (pk) column
0985:                                joinColumn
0986:                                        .setReferencedColumnName(localColumnName.column);
0987:                                field.getRelatedField().getJoinColumn().add(
0988:                                        joinColumn);
0989:                            }
0990:                        } else if (field instanceof  ManyToOne) {
0991:                            for (ColumnPair columnPair : cmrFieldMapping
0992:                                    .getColumnPair()) {
0993:                                SunColumnName localColumnName = new SunColumnName(
0994:                                        columnPair.getColumnName().get(0),
0995:                                        table.getName());
0996:                                SunColumnName referencedColumnName = new SunColumnName(
0997:                                        columnPair.getColumnName().get(1),
0998:                                        table.getName());
0999:
1000:                                // if user specified in reverse order, swap
1001:                                if (localColumnName.table != null) {
1002:                                    SunColumnName temp = localColumnName;
1003:                                    localColumnName = referencedColumnName;
1004:                                    referencedColumnName = temp;
1005:                                }
1006:
1007:                                JoinColumn joinColumn = new JoinColumn();
1008:                                joinColumn.setName(localColumnName.column);
1009:                                joinColumn
1010:                                        .setReferencedColumnName(referencedColumnName.column);
1011:                                field.getJoinColumn().add(joinColumn);
1012:                            }
1013:                        } else {
1014:                            // skip the non owning side
1015:                            if (field.getMappedBy() != null)
1016:                                continue;
1017:
1018:                            JoinTable joinTable = new JoinTable();
1019:                            field.setJoinTable(joinTable);
1020:                            for (ColumnPair columnPair : cmrFieldMapping
1021:                                    .getColumnPair()) {
1022:                                SunColumnName localColumnName = new SunColumnName(
1023:                                        columnPair.getColumnName().get(0),
1024:                                        table.getName());
1025:                                SunColumnName joinTableColumnName = new SunColumnName(
1026:                                        columnPair.getColumnName().get(1),
1027:                                        table.getName());
1028:
1029:                                if (localColumnName.table == null
1030:                                        || joinTableColumnName.table == null) {
1031:                                    // if user specified in reverse order, swap
1032:                                    if (localColumnName.table != null) {
1033:                                        SunColumnName temp = localColumnName;
1034:                                        localColumnName = joinTableColumnName;
1035:                                        joinTableColumnName = temp;
1036:                                    }
1037:
1038:                                    // join table is the table name of the referenced column
1039:                                    joinTable
1040:                                            .setName(joinTableColumnName.table);
1041:
1042:                                    JoinColumn joinColumn = new JoinColumn();
1043:                                    joinColumn
1044:                                            .setName(joinTableColumnName.column);
1045:                                    joinColumn
1046:                                            .setReferencedColumnName(localColumnName.column);
1047:                                    joinTable.getJoinColumn().add(joinColumn);
1048:                                } else {
1049:                                    // if user specified in reverse order, swap
1050:                                    if (localColumnName.table.equals(joinTable
1051:                                            .getName())) {
1052:                                        SunColumnName temp = localColumnName;
1053:                                        localColumnName = joinTableColumnName;
1054:                                        joinTableColumnName = temp;
1055:                                    }
1056:
1057:                                    JoinColumn joinColumn = new JoinColumn();
1058:                                    joinColumn
1059:                                            .setName(joinTableColumnName.column);
1060:                                    joinColumn
1061:                                            .setReferencedColumnName(localColumnName.column);
1062:                                    joinTable.getInverseJoinColumn().add(
1063:                                            joinColumn);
1064:                                }
1065:
1066:                            }
1067:                        }
1068:                    }
1069:                }
1070:            }
1071:
1072:            public String convertToEjbQl(String abstractSchemaName,
1073:                    String queryParams, String queryFilter) {
1074:                return convertToEjbQl(abstractSchemaName, Collections
1075:                        .<String> emptyList(), queryParams, queryFilter);
1076:            }
1077:
1078:            public String convertToEjbQl(String abstractSchemaName,
1079:                    Collection<String> cmpFields, String queryParams,
1080:                    String queryFilter) {
1081:                List<List<String>> variableNames = parseQueryParamters(queryParams);
1082:
1083:                StringBuilder ejbQl = new StringBuilder();
1084:                ejbQl.append("SELECT OBJECT(o) FROM ").append(
1085:                        abstractSchemaName).append(" AS o");
1086:                String filter = convertToEjbQlFilter(cmpFields, variableNames,
1087:                        queryFilter);
1088:                if (filter != null) {
1089:                    ejbQl.append(" WHERE ").append(filter);
1090:                }
1091:                return ejbQl.toString();
1092:            }
1093:
1094:            private List<List<String>> parseQueryParamters(String queryParams) {
1095:                if (queryParams == null)
1096:                    return Collections.emptyList();
1097:
1098:                List bits = Collections.list(new StringTokenizer(queryParams,
1099:                        " \t\n\r\f,", false));
1100:                List<List<String>> params = new ArrayList<List<String>>(bits
1101:                        .size() / 2);
1102:                for (int i = 0; i < bits.size(); i++) {
1103:                    String type = resolveType((String) bits.get(i));
1104:                    String param = (String) bits.get(++i);
1105:                    params.add(Arrays.asList(type, param));
1106:                }
1107:                return params;
1108:            }
1109:
1110:            private String resolveType(String type) {
1111:                try {
1112:                    ClassLoader.getSystemClassLoader().loadClass(type);
1113:                    return type;
1114:                } catch (ClassNotFoundException e) {
1115:                }
1116:                try {
1117:                    String javaLangType = "java.lang" + type;
1118:                    ClassLoader.getSystemClassLoader().loadClass(javaLangType);
1119:                    return javaLangType;
1120:                } catch (ClassNotFoundException e) {
1121:                }
1122:                return type;
1123:            }
1124:
1125:            private String convertToEjbQlFilter(Collection<String> cmpFields,
1126:                    List<List<String>> queryParams, String queryFilter) {
1127:                if (queryFilter == null)
1128:                    return null;
1129:
1130:                Map<String, String> variableMap = new TreeMap<String, String>();
1131:                for (String cmpField : cmpFields) {
1132:                    variableMap.put(cmpField, "o." + cmpField);
1133:                }
1134:                for (int i = 0; i < queryParams.size(); i++) {
1135:                    List<String> param = queryParams.get(i);
1136:                    variableMap.put(param.get(1), "?" + (i + 1));
1137:                }
1138:
1139:                Map<String, String> symbolMap = new TreeMap<String, String>();
1140:                symbolMap.put("&&", "and");
1141:                symbolMap.put("||", "or");
1142:                symbolMap.put("!", "not");
1143:                symbolMap.put("==", "=");
1144:                symbolMap.put("!=", "<>");
1145:
1146:                StringBuilder ejbQlFilter = new StringBuilder(queryFilter
1147:                        .length() * 2);
1148:                List<String> tokens = tokenize(queryFilter);
1149:                for (String token : tokens) {
1150:                    String mappedToken = symbolMap.get(token);
1151:                    if (mappedToken == null) {
1152:                        mappedToken = variableMap.get(token);
1153:                    }
1154:
1155:                    if (mappedToken != null) {
1156:                        ejbQlFilter.append(mappedToken);
1157:                    } else {
1158:                        ejbQlFilter.append(token);
1159:                    }
1160:                    ejbQlFilter.append(" ");
1161:                }
1162:                String filter = ejbQlFilter.toString().trim();
1163:                if (filter.equalsIgnoreCase("true")) {
1164:                    return null;
1165:                } else {
1166:                    return filter;
1167:                }
1168:            }
1169:
1170:            private static enum TokenType {
1171:                WHITESPACE, SYMBOL, NORMAL
1172:            }
1173:
1174:            private List<String> tokenize(String queryFilter) {
1175:                LinkedList<String> tokens = new LinkedList<String>();
1176:                List bits = Collections.list(new StringTokenizer(queryFilter,
1177:                        " \t\n\r\f()&|<>=!~+-/*", true));
1178:
1179:                boolean inWitespace = false;
1180:                String currentSymbol = "";
1181:                for (int i = 0; i < bits.size(); i++) {
1182:                    TokenType tokenType;
1183:                    String bit = (String) bits.get(i);
1184:                    switch (bit.charAt(0)) {
1185:                    case ' ':
1186:                    case '\t':
1187:                    case '\n':
1188:                    case '\r':
1189:                    case '\f':
1190:                        inWitespace = true;
1191:                        tokenType = TokenType.WHITESPACE;
1192:                        break;
1193:                    case '&':
1194:                    case '|':
1195:                    case '=':
1196:                    case '>':
1197:                    case '<':
1198:                    case '!':
1199:                        // symbols are blindly coalesced so you can end up with nonsence like +-=+
1200:                        currentSymbol += bit.charAt(0);
1201:                        tokenType = TokenType.SYMBOL;
1202:                        break;
1203:                    default:
1204:                        tokenType = TokenType.NORMAL;
1205:                    }
1206:                    if (tokenType != TokenType.WHITESPACE && inWitespace) {
1207:                        // sequences of white space are simply removed
1208:                        inWitespace = false;
1209:                    }
1210:                    if (tokenType != TokenType.SYMBOL
1211:                            && currentSymbol.length() > 0) {
1212:                        tokens.add(currentSymbol);
1213:                        currentSymbol = "";
1214:                    }
1215:                    if (tokenType == TokenType.NORMAL) {
1216:                        tokens.add(bit);
1217:                    }
1218:                }
1219:                // add saved symobl if we have one
1220:                if (currentSymbol.length() > 0) {
1221:                    tokens.add(currentSymbol);
1222:                    currentSymbol = "";
1223:                }
1224:                // strip off leading space
1225:                if (tokens.getFirst().equals(" ")) {
1226:                    tokens.removeFirst();
1227:                }
1228:                return tokens;
1229:            }
1230:
1231:            private class SunColumnName {
1232:                private final String table;
1233:                private final String column;
1234:
1235:                public SunColumnName(ColumnName columnName,
1236:                        String primaryTableName) {
1237:                    this (columnName.getvalue(), primaryTableName);
1238:                }
1239:
1240:                public SunColumnName(String fullName, String primaryTableName) {
1241:                    int dot = fullName.indexOf('.');
1242:                    if (dot > 0) {
1243:                        String t = fullName.substring(0, dot);
1244:                        if (primaryTableName.equals(t)) {
1245:                            table = null;
1246:                        } else {
1247:                            table = t;
1248:                        }
1249:                        column = fullName.substring(dot + 1);
1250:                    } else {
1251:                        table = null;
1252:                        column = fullName;
1253:                    }
1254:                }
1255:            }
1256:
1257:            private class EntityData {
1258:                private final Entity entity;
1259:                private final Map<String, Id> ids = new TreeMap<String, Id>();
1260:                private final Map<String, Field> fields = new TreeMap<String, Field>();
1261:                private final Map<String, RelationField> relations = new TreeMap<String, RelationField>();
1262:
1263:                public EntityData(Entity entity) {
1264:                    if (entity == null)
1265:                        throw new NullPointerException("entity is null");
1266:                    this .entity = entity;
1267:
1268:                    Attributes attributes = entity.getAttributes();
1269:                    if (attributes != null) {
1270:                        for (Id id : attributes.getId()) {
1271:                            String name = id.getName();
1272:                            ids.put(name, id);
1273:                            fields.put(name, id);
1274:                        }
1275:
1276:                        for (Basic basic : attributes.getBasic()) {
1277:                            String name = basic.getName();
1278:                            fields.put(name, basic);
1279:                        }
1280:
1281:                        for (RelationField relationField : attributes
1282:                                .getOneToOne()) {
1283:                            String name = relationField.getName();
1284:                            relations.put(name, relationField);
1285:                        }
1286:
1287:                        for (RelationField relationField : attributes
1288:                                .getOneToMany()) {
1289:                            String name = relationField.getName();
1290:                            relations.put(name, relationField);
1291:                        }
1292:
1293:                        for (RelationField relationField : attributes
1294:                                .getManyToOne()) {
1295:                            String name = relationField.getName();
1296:                            relations.put(name, relationField);
1297:                        }
1298:
1299:                        for (RelationField relationField : attributes
1300:                                .getManyToMany()) {
1301:                            String name = relationField.getName();
1302:                            relations.put(name, relationField);
1303:                        }
1304:                    }
1305:
1306:                    for (AttributeOverride attributeOverride : entity
1307:                            .getAttributeOverride()) {
1308:                        String name = attributeOverride.getName();
1309:                        fields.put(name, attributeOverride);
1310:                    }
1311:                }
1312:
1313:                public boolean hasPkColumnMapping(String column) {
1314:                    for (Id id : ids.values()) {
1315:                        if (column.equals(id.getColumn().getName())) {
1316:                            return true;
1317:                        }
1318:                    }
1319:                    return false;
1320:                }
1321:            }
1322:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.