1
16
17
20
21 package org.apache.wsrp4j.consumer.driver;
22
23 import java.net.URL;
24 import java.util.Hashtable;
25
26 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_PortletManagement_PortType;
27 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_Registration_PortType;
28 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_ServiceDescription_PortType;
29 import oasis.names.tc.wsrp.v1.types.GetServiceDescription;
30 import oasis.names.tc.wsrp.v1.types.InvalidRegistrationFault;
31 import oasis.names.tc.wsrp.v1.types.ModifyRegistration;
32 import oasis.names.tc.wsrp.v1.types.PortletDescription;
33 import oasis.names.tc.wsrp.v1.types.RegistrationContext;
34 import oasis.names.tc.wsrp.v1.types.RegistrationData;
35 import oasis.names.tc.wsrp.v1.types.RegistrationState;
36 import oasis.names.tc.wsrp.v1.types.ReturnAny;
37 import oasis.names.tc.wsrp.v1.types.ServiceDescription;
38 import oasis.names.tc.wsrp.v1.wsdl.WSRPServiceLocator;
39
40 import org.apache.wsrp4j.consumer.Producer;
41 import org.apache.wsrp4j.exception.ErrorCodes;
42 import org.apache.wsrp4j.exception.WSRPException;
43 import org.apache.wsrp4j.exception.WSRPXHelper;
44 import org.apache.wsrp4j.log.LogManager;
45 import org.apache.wsrp4j.log.Logger;
46 import org.apache.wsrp4j.util.ParameterChecker;
47 import org.apache.wsrp4j.util.StateChangedServiceImpl;
48
49
59 public class ProducerImpl extends StateChangedServiceImpl implements Producer {
60
61 private String name = null;
63
64 private String producerID = null;
65
66 private String description = null;
67
68 private String registrationURL;
70
71 private String serviceDescriptionURL;
72
73 private String markupURL;
74
75 private String portletManagementURL;
76
77 private boolean registrationRequired = false;
79
80 private RegistrationContext registrationContext = null;
81
82 private RegistrationData consumerRegData = null;
83
84 private ServiceDescription serviceDescription = null;
86
87 private Hashtable portletDesc = null;
88
89 private WSRP_v1_ServiceDescription_PortType serviceDescriptionInterface = null;
91
92 private WSRP_v1_Registration_PortType registrationInterface = null;
93
94 private WSRP_v1_PortletManagement_PortType portletManagementInterface = null;
95
96 private Logger logger = null;
98
99 private ParameterChecker checker = null;
100
101
105 public ProducerImpl() {
106 this.portletDesc = new Hashtable();
107 logger = LogManager.getLogManager().getLogger(getClass());
108 checker = new ParameterChecker();
109
110 }
111
112
115 public ProducerImpl(String producerID, String markupURL,
116 String serviceDescriptionURL) throws WSRPException {
117
118 this();
119 this.producerID = producerID;
120
121 if (markupURL != null) {
122 this.markupURL = markupURL;
123 }
124 else {
125 WSRPXHelper.throwX(logger, Logger.ERROR, "init",
126 ErrorCodes.MISSING_MARKUP_PORT);
127 }
128
129 if (serviceDescriptionURL != null) {
130
131 this.serviceDescriptionURL = serviceDescriptionURL;
132 initServiceDescInterface(serviceDescriptionURL);
133
134 }
135 else {
136 WSRPXHelper.throwX(logger, Logger.ERROR, "init",
137 ErrorCodes.MISSING_SERVCICE_DESC_PORT);
138 }
139
140 }
141
142
145 public ProducerImpl(String producerID, String markupURL,
146 String serviceDescriptionURL, String registrationURL,
147 String portletManagementURL, RegistrationData registrationData)
148 throws WSRPException {
149
150 this(producerID, markupURL, serviceDescriptionURL);
151
152 if (registrationURL != null) {
153
154 initRegistrationInterface(registrationURL);
155 this.registrationURL = registrationURL;
156 this.consumerRegData = registrationData;
157 }
158
159 if (portletManagementURL != null) {
160 this.portletManagementURL = portletManagementURL;
161 initPortletManagementInterface(portletManagementURL);
162 }
163 }
164
165
170 public void initServiceDescInterface(String serviceDescriptionURL)
171 throws WSRPException {
172
173 this.serviceDescriptionURL = serviceDescriptionURL;
175
176 try {
177 WSRPServiceLocator serviceLocator = new WSRPServiceLocator();
178 serviceDescriptionInterface = serviceLocator
179 .getWSRPServiceDescriptionService(new URL(
180 serviceDescriptionURL));
181
182 }
183 catch (javax.xml.rpc.ServiceException xmlEx) {
184
185 WSRPXHelper.throwX(logger, Logger.ERROR,
186 "initServiceDescInterface",
187 ErrorCodes.INIT_OF_SERVICE_DESC_PORT_FAILED, xmlEx);
188
189 }
190 catch (java.net.MalformedURLException urlEx) {
191
192 WSRPXHelper.throwX(logger, Logger.ERROR,
193 "initServiceDescInterface",
194 ErrorCodes.INVALID_URL_OF_SERVICE_DESC_PORT, urlEx);
195
196 }
197 }
198
199
204 public void initRegistrationInterface(String registrationURL)
205 throws WSRPException {
206
207 this.registrationURL = registrationURL;
209
210 try {
211 WSRPServiceLocator serviceLocator = new WSRPServiceLocator();
212 registrationInterface = serviceLocator
213 .getWSRPRegistrationService(new URL(registrationURL));
214
215 }
216 catch (javax.xml.rpc.ServiceException xmlEx) {
217
218 WSRPXHelper.throwX(logger, Logger.ERROR,
219 "initRegistrationInterface",
220 ErrorCodes.INIT_OF_REGISTRATION_PORT_FAILED, xmlEx);
221
222 }
223 catch (java.net.MalformedURLException urlEx) {
224
225 WSRPXHelper.throwX(logger, Logger.ERROR,
226 "initRegistrationInterface",
227 ErrorCodes.INVALID_URL_OF_REGISTRATION_PORT, urlEx);
228
229 }
230 }
231
232
237 public String getServiceDescriptionInterfaceEndpoint() {
238 return this.serviceDescriptionURL;
239 }
240
241
246 public void setServiceDescriptionInterfaceEndpoint(String url) {
247 serviceDescriptionURL = url;
248 stateChanged();
249 }
250
251
256 public String getRegistrationInterfaceEndpoint() {
257 return registrationURL;
258 }
259
260
265 public void setRegistrationInterfaceEndpoint(String url) {
266 registrationURL = url;
267 stateChanged();
268 }
269
270
275 public String getID() {
276 return producerID;
277 }
278
279
284 public void setID(String id) {
285 producerID = id;
286 stateChanged();
287 }
288
289
294 public String getName() {
295 return name;
296 }
297
298
303 public void setName(String name) {
304 this.name = name;
305 stateChanged();
306 }
307
308
313 public String getDescription() {
314 return this.description;
315 }
316
317
322 public void setDescription(String description) {
323 this.description = description;
324 stateChanged();
325 }
326
327
332 public boolean isRegistrationRequired() {
333 return this.registrationRequired;
334 }
335
336
341 public void setIsRegistrationRequired(boolean registrationRequired) {
342 this.registrationRequired = registrationRequired;
343 stateChanged();
344 }
345
346
351 public RegistrationData getRegistrationData() {
352 return consumerRegData;
353 }
354
355
360 public void setRegistrationData(RegistrationData regData) {
361 consumerRegData = regData;
362 stateChanged();
363 }
364
365
371 public RegistrationContext getRegistrationContext() {
372 return this.registrationContext;
373 }
374
375
380 public void setRegistrationContext(RegistrationContext registrationContext) {
381 if (registrationContext != null) {
382 this.registrationContext = registrationContext;
383 stateChanged();
384 }
385 }
386
387
390 public ServiceDescription getServiceDescription() throws WSRPException {
391 return getServiceDescription(false);
392 }
393
394
402 public ServiceDescription getServiceDescription(boolean newRequest)
403 throws WSRPException {
404 if (this.serviceDescription == null || newRequest) {
405 GetServiceDescription request = new GetServiceDescription();
406
407 if (registrationContext != null) {
408 request.setRegistrationContext(registrationContext);
409 }
410
411 request.setDesiredLocales(null);
415
416 ServiceDescription response = null;
417 try {
418
419 response = serviceDescriptionInterface
420 .getServiceDescription(request);
421
422 if (response != null) {
423
424 try {
425
426 checker.check(response);
427
428 }
429 catch (java.rmi.RemoteException wsrpFault) {
430
431 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
432
433 }
434
435 if (registrationContext == null
436 && response.isRequiresRegistration()) {
437 register(consumerRegData);
438 getServiceDescription(true);
439
440 }
441 else {
442 setServiceDescription(response);
443 }
444
445 }
446 else {
447
448 WSRPXHelper.throwX(logger, Logger.ERROR,
449 "getServiceDescription",
450 ErrorCodes.INVALID_SERVICE_DESCRIPTION);
451 }
452
453 }
454 catch (InvalidRegistrationFault invalidReg) {
455
456 register(consumerRegData);
459 getServiceDescription(true);
460
461 }
462 catch (java.rmi.RemoteException wsrpFault) {
463
464 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
465
466 }
467 }
468
469 return this.serviceDescription;
470 }
471
472
481 public PortletDescription getPortletDescription(String portletHandle)
482 throws WSRPException {
483
484 PortletDescription desc = null;
485 if (portletHandle != null) {
486
487 if (serviceDescription == null) {
488
489 getServiceDescription();
491 }
492
493 desc = (PortletDescription) portletDesc.get(portletHandle);
494
495 if (desc == null) {
496
497 WSRPXHelper.throwX(logger, Logger.ERROR,
498 "getPortletDescription",
499 ErrorCodes.INVALID_PORTLET_HANDLE);
500 }
501 }
502
503 return desc;
504 }
505
506
512 public void setServiceDescription(ServiceDescription serviceDescription) {
513 if (serviceDescription != null) {
514 this.serviceDescription = serviceDescription;
515 updatePortletDescriptions(serviceDescription);
516 setIsRegistrationRequired(serviceDescription
517 .isRequiresRegistration());
518 }
519 }
520
521
529 public void addPortletDescription(PortletDescription portletDescription) {
530 if (portletDescription != null) {
531 this.portletDesc.put(portletDescription.getPortletHandle(),
532 portletDescription);
533 }
534 }
535
536 private void updatePortletDescriptions(ServiceDescription serviceDescription) {
537 if (serviceDescription != null) {
538 PortletDescription[] offeredPortlets = serviceDescription
539 .getOfferedPortlets();
540 if (offeredPortlets != null) {
541 this.portletDesc.clear();
542
543 for (int i = 0; i < offeredPortlets.length; i++) {
544 if (offeredPortlets[i] != null) {
545 addPortletDescription(offeredPortlets[i]);
546 }
547
548 }
549 }
550 }
551 }
552
553
562 public RegistrationContext register(RegistrationData registrationData)
563 throws WSRPException {
564
565 if (registrationData != null && this.registrationInterface != null) {
566
567 try {
568
569 RegistrationContext regContext = registrationInterface
570 .register(registrationData);
571 checker.check(regContext, false);
572 setRegistrationContext(regContext);
573
574 }
575 catch (java.rmi.RemoteException wsrpFault) {
576
577 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
578
579 }
580
581 }
582 else {
583
584 }
586
587 return this.registrationContext;
588 }
589
590
599 public RegistrationState modifyRegistration(
600 RegistrationData registrationData) throws WSRPException {
601
602 RegistrationState newState = null;
603
604 try {
605 if (registrationData != null && this.registrationInterface != null) {
606
607 ModifyRegistration request = new ModifyRegistration();
608 if (registrationContext != null) {
609 request.setRegistrationContext(registrationContext);
610 }
611 request.setRegistrationData(registrationData);
612
613 newState = registrationInterface.modifyRegistration(request);
614
615 getRegistrationContext().setRegistrationState(
616 newState.getRegistrationState());
617 stateChanged();
618
619 }
620 else {
621
622 }
624 }
625 catch (java.rmi.RemoteException wsrpFault) {
626
627 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
628
629 }
630
631 return newState;
632 }
633
634
639 public ReturnAny deregister() throws WSRPException {
640 ReturnAny any = null;
641
642 try {
643 if (registrationContext != null
644 && this.registrationInterface != null) {
645
646 any = registrationInterface
647 .deregister(this.registrationContext);
648 registrationContext = null;
649 registrationRequired = false;
650
651 stateChanged();
652
653 }
654 else {
655 }
658
659 }
660 catch (java.rmi.RemoteException wsrpFault) {
661
662 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
663 }
664
665 return any;
666 }
667
668
671 public String getMarkupInterfaceEndpoint() {
672 return markupURL;
673 }
674
675
678 public void setMarkupInterfaceEndpoint(String url) {
679 markupURL = url;
680 stateChanged();
681 }
682
683
686 public String getPortletManagementInterfaceEndpoint() {
687 return portletManagementURL;
688 }
689
690
693 public void setPortletManagementInterfaceEndpoint(String url) {
694 portletManagementURL = url;
695 stateChanged();
696 }
697
698 public void initPortletManagementInterface(String portletManagementURL)
699 throws WSRPException {
700 final String MN = "initPortletManagementInterface";
701
702 this.portletManagementURL = portletManagementURL;
704
705 try {
706 WSRPServiceLocator serviceLocator = new WSRPServiceLocator();
707 portletManagementInterface = serviceLocator
708 .getWSRPPortletManagementService(new URL(
709 portletManagementURL));
710 }
711 catch (javax.xml.rpc.ServiceException xmlEx) {
712
713 WSRPXHelper.throwX(logger, Logger.ERROR, MN,
714 ErrorCodes.INIT_OF_PORTLET_MGMT_PORT_FAILED, xmlEx);
715
716 }
717 catch (java.net.MalformedURLException urlEx) {
718
719 WSRPXHelper.throwX(logger, Logger.ERROR, MN,
720 ErrorCodes.INVALID_URL_OF_PORTLET_MGMT_PORT, urlEx);
721
722 }
723 }
724
725 public WSRP_v1_PortletManagement_PortType getPortletManagementInterface() {
726 return portletManagementInterface;
727 }
728
729 public WSRP_v1_Registration_PortType getRegistrationInterface() {
730 return registrationInterface;
731 }
732
733 public WSRP_v1_ServiceDescription_PortType getServiceDescriptionInterface() {
734 return serviceDescriptionInterface;
735 }
736
737 public boolean isPortletManagementInferfaceSupported() {
738 if (portletManagementURL != null) {
739 return true;
740 }
741 return false;
742 }
743
744 public boolean isRegistrationInterfaceSupported() {
745 if (registrationURL != null) {
746 return true;
747 }
748 return false;
749 }
750
751 }