From e876e28e141ceb2e37470e3aa675db1d1c7fc301 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 25 Jun 2017 19:25:35 +0200 Subject: [PATCH] Please cherry-pick: - sadly, Glassfish and Payara are both not so super-flexible to allow JNDI lookups in constructors. You have to either move that code to a @PostConstruct annotated method in bean (=controller) classes or do it on-demand. - @PostConstruct, @Observes and so on are not processed outside beans, next "limitation" (or not wanted/intended/possible?). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../PizzaAdminContactWebRequestBean.java | 32 ++++---- .../contact/PizzaContactConverter.java | 28 ++++--- .../country/PizzaCountryConverter.java | 30 ++++--- .../mobile/PizzaMobileConverter.java | 28 ++++--- .../PizzaMobileProviderConverter.java | 28 ++++--- .../converter/user/PizzaUserConverter.java | 28 ++++--- .../birthday/PizzaBirthdayValidator.java | 4 +- .../PizzaEmailAddressValidator.java | 32 +++++--- .../PizzaPrivacyTermsCheckboxValidator.java | 1 + .../validator/user/PizzaUserIdValidator.java | 81 +++++-------------- 10 files changed, 142 insertions(+), 150 deletions(-) diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java index b6acfa1a..da791cd9 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java @@ -33,9 +33,9 @@ import javax.naming.NamingException; import org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; +import org.mxchange.jcontacts.contact.ContactUtils; import org.mxchange.jcontacts.contact.UserContact; import org.mxchange.jcontacts.contact.title.PersonalTitle; -import org.mxchange.jcontacts.contact.ContactUtils; import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; @@ -67,6 +67,11 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Academic academicTitle + */ + private String academicTitle; + /** * An event fired when the administrator has added a new contact */ @@ -226,11 +231,6 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme */ private String street; - /** - * Academic academicTitle - */ - private String academicTitle; - /** * An event fired when the administrator has updated contact data */ @@ -542,6 +542,16 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme return number; } + @Override + public String getAcademicTitle () { + return this.academicTitle; + } + + @Override + public void setAcademicTitle (final String academicTitle) { + this.academicTitle = academicTitle; + } + @Override @SuppressWarnings ("ReturnOfDateField") public Date getBirthday () { @@ -785,16 +795,6 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme this.street = street; } - @Override - public String getAcademicTitle () { - return this.academicTitle; - } - - @Override - public void setAcademicTitle (final String academicTitle) { - this.academicTitle = academicTitle; - } - @Override public Integer getZipCode () { return this.zipCode; diff --git a/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java b/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java index 2265954f..af15d838 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java @@ -46,17 +46,6 @@ public class PizzaContactConverter implements Converter { * Initialization of this converter */ public PizzaContactConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // ... and user controller - this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } } @Override @@ -70,6 +59,23 @@ public class PizzaContactConverter implements Converter { return null; } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.contactBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // ... and user controller + this.contactBean = (ContactSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Init instance Contact contact = null; diff --git a/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java b/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java index 6ccfa5c7..21e844c0 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java @@ -41,23 +41,12 @@ public class PizzaCountryConverter implements Converter { /** * Country bean */ - private CountrySingletonBeanRemote countryBean; + private CountrySingletonBeanRemote countryBean = null; /** * Initialization of this converter */ public PizzaCountryConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // ... and country bean - this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } } @Override @@ -71,6 +60,23 @@ public class PizzaCountryConverter implements Converter { return null; } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.countryBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // ... and country bean + this.countryBean = (CountrySingletonBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Get full list List countryList = this.countryBean.allCountries(); diff --git a/src/java/org/mxchange/pizzaapplication/converter/mobile/PizzaMobileConverter.java b/src/java/org/mxchange/pizzaapplication/converter/mobile/PizzaMobileConverter.java index 70287ae9..e448d8f9 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/mobile/PizzaMobileConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/mobile/PizzaMobileConverter.java @@ -47,17 +47,6 @@ public class PizzaMobileConverter implements Converter { * Initialization of this converter */ public PizzaMobileConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // ... and user controller - this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } } @Override @@ -71,6 +60,23 @@ public class PizzaMobileConverter implements Converter { return null; } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.phoneBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // ... and user controller + this.phoneBean = (PhoneSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Init instance DialableMobileNumber mobile = null; diff --git a/src/java/org/mxchange/pizzaapplication/converter/mobileprovider/PizzaMobileProviderConverter.java b/src/java/org/mxchange/pizzaapplication/converter/mobileprovider/PizzaMobileProviderConverter.java index 87f9ac6b..602e4641 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/mobileprovider/PizzaMobileProviderConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/mobileprovider/PizzaMobileProviderConverter.java @@ -47,17 +47,6 @@ public class PizzaMobileProviderConverter implements Converter { * Initialization of this converter */ public PizzaMobileProviderConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - /// and SMS provider controller - this.mobileRemoteBean = (MobileProviderSingletonBeanRemote) context.lookup("java:global/pizzaservice-ejb/mobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } } @Override @@ -71,6 +60,23 @@ public class PizzaMobileProviderConverter implements Converter { return null; } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.mobileRemoteBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + /// and mobile provider controller + this.mobileRemoteBean = (MobileProviderSingletonBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/mobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Get full list List providerList = this.mobileRemoteBean.allMobileProvider(); diff --git a/src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java b/src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java index d33bafe1..e1749402 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java @@ -46,17 +46,6 @@ public class PizzaUserConverter implements Converter { * Initialization of this converter */ public PizzaUserConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // ... and user controller - this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } } @Override @@ -70,6 +59,23 @@ public class PizzaUserConverter implements Converter { return null; } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.userBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // ... and user controller + this.userBean = (UserSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new RuntimeException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Init instance User user = null; diff --git a/src/java/org/mxchange/pizzaapplication/validator/birthday/PizzaBirthdayValidator.java b/src/java/org/mxchange/pizzaapplication/validator/birthday/PizzaBirthdayValidator.java index 1a3fbdfe..888b5b75 100644 --- a/src/java/org/mxchange/pizzaapplication/validator/birthday/PizzaBirthdayValidator.java +++ b/src/java/org/mxchange/pizzaapplication/validator/birthday/PizzaBirthdayValidator.java @@ -49,10 +49,10 @@ public class PizzaBirthdayValidator extends BaseDateValidator implements Validat // Try to get it try { // Get initial context - Context context = new InitialContext(); + Context initialContext = new InitialContext(); } catch (final NamingException ex) { // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N } } diff --git a/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java b/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java index fff7640f..1a0ceff4 100644 --- a/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java +++ b/src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java @@ -21,10 +21,10 @@ import java.util.regex.Pattern; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; +import javax.faces.convert.ConverterException; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; -import javax.faces.view.facelets.FaceletException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -47,23 +47,12 @@ public class PizzaEmailAddressValidator extends BaseStringValidator implements V /** * Contact session-scoped bean */ - private final ContactSessionBeanRemote contactBean; + private ContactSessionBeanRemote contactBean; /** * Default constructor */ public PizzaEmailAddressValidator () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw again - throw new FaceletException(e); - } } @Override @@ -91,6 +80,23 @@ public class PizzaEmailAddressValidator extends BaseStringValidator implements V throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message)); } + synchronized (this) { + // Is the EJB instanciated? + if (null == this.contactBean) { + // Try it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // Try to lookup + this.contactBean = (ContactSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + } + // Get client id (aka form id) String clientId = component.getClientId(); diff --git a/src/java/org/mxchange/pizzaapplication/validator/privacy_terms/PizzaPrivacyTermsCheckboxValidator.java b/src/java/org/mxchange/pizzaapplication/validator/privacy_terms/PizzaPrivacyTermsCheckboxValidator.java index ba3f108d..378c968a 100644 --- a/src/java/org/mxchange/pizzaapplication/validator/privacy_terms/PizzaPrivacyTermsCheckboxValidator.java +++ b/src/java/org/mxchange/pizzaapplication/validator/privacy_terms/PizzaPrivacyTermsCheckboxValidator.java @@ -44,4 +44,5 @@ public class PizzaPrivacyTermsCheckboxValidator extends BaseBooleanValidator imp // Pre-validation (example: not null, not a string, empty string ...) super.preValidate(context, component, value, requiredFields, false); } + } diff --git a/src/java/org/mxchange/pizzaapplication/validator/user/PizzaUserIdValidator.java b/src/java/org/mxchange/pizzaapplication/validator/user/PizzaUserIdValidator.java index ba8df1a7..aa6e1de8 100644 --- a/src/java/org/mxchange/pizzaapplication/validator/user/PizzaUserIdValidator.java +++ b/src/java/org/mxchange/pizzaapplication/validator/user/PizzaUserIdValidator.java @@ -17,9 +17,6 @@ package org.mxchange.pizzaapplication.validator.user; import java.text.MessageFormat; -import java.util.Set; -import java.util.TreeSet; -import javax.enterprise.event.Observes; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; @@ -31,8 +28,6 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcoreee.validator.number.BaseLongValidator; -import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.UserSessionBeanRemote; /** @@ -43,11 +38,6 @@ import org.mxchange.jusercore.model.user.UserSessionBeanRemote; @FacesValidator ("UserIdValidator") public class PizzaUserIdValidator extends BaseLongValidator implements Validator { - /** - * Cached user status - */ - private static final Set cachedStatus = new TreeSet<>(); - /** * Serial number */ @@ -62,46 +52,6 @@ public class PizzaUserIdValidator extends BaseLongValidator implements Validator * Initialization of this converter */ public PizzaUserIdValidator () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // ... and user controller - this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } - } - - /** - * Event fired when the user registration is complete - *

- * @param event User registration event - */ - public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getRegisteredUser() == null) { - // Throw NPE again - throw new NullPointerException("event.user is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.user.userId is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N - } - - // Get user instance - User registeredUser = event.getRegisteredUser(); - - // Update cache - PizzaUserIdValidator.cachedStatus.add(registeredUser.getUserId()); - } @Override @@ -115,26 +65,31 @@ public class PizzaUserIdValidator extends BaseLongValidator implements Validator // Cast value Long userId = (Long) value; - // Define variable - Boolean ifUserExists; - - // Is a map entry there? - if (PizzaUserIdValidator.cachedStatus.contains(userId)) { - // Get from cache - ifUserExists = Boolean.TRUE; - } else { - // Get status - ifUserExists = this.userBean.ifUserIdExists(userId); + synchronized (this) { + // Is the EJB instanciated? + if (null == this.userBean) { + // Try to get it + try { + // Get initial context + Context initialContext = new InitialContext(); + + // ... and user controller + this.userBean = (UserSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } } + // Define variable + Boolean ifUserExists = this.userBean.ifUserIdExists(userId); + // Is the user id valid? if (!ifUserExists) { // Is not valid throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N } - - // Add to cache if valid - PizzaUserIdValidator.cachedStatus.add(userId); } } -- 2.39.5