]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java
Don't cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / country / PizzaCountryConverter.java
index e63d29f26e633982cb11caa928a9373c4e538c5e..fda1cc20609dac2e982c2832939f313affae4387 100644 (file)
@@ -18,12 +18,16 @@ package org.mxchange.pizzaapplication.converter.country;
 
 import java.util.List;
 import java.util.Objects;
-import javax.ejb.EJB;
+import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.Converter;
 import javax.faces.convert.ConverterException;
 import javax.faces.convert.FacesConverter;
+import javax.faces.validator.ValidatorException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import org.mxchange.jcountry.model.data.Country;
 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
 
@@ -38,17 +42,24 @@ public class PizzaCountryConverter implements Converter<Country> {
        /**
         * Country bean
         */
-       @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote")
-       private CountrySingletonBeanRemote countryBean;
-
-       /**
-        * Default constructor
-        */
-       public PizzaCountryConverter () {
-       }
+       private static CountrySingletonBeanRemote COUNTRY_BEAN;
 
        @Override
        public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+               // Is the instance there?
+               if (COUNTRY_BEAN == null) {
+                       try {
+                               // Not yet, attempt lookup
+                               final Context initial = new InitialContext();
+
+                               // Lookup EJB
+                               COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote");
+                       } catch (final NamingException ex) {
+                               // Throw it again
+                               throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
+                       }
+               }
+
                // Is the value null or empty?
                if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
                        // Warning message
@@ -59,7 +70,7 @@ public class PizzaCountryConverter implements Converter<Country> {
                }
 
                // Get full list
-               List<Country> countryList = this.countryBean.allCountries();
+               final List<Country> countryList = COUNTRY_BEAN.allCountries();
 
                // Init value
                Country country = null;
@@ -67,7 +78,7 @@ public class PizzaCountryConverter implements Converter<Country> {
                // Try this better
                try {
                        // Convert it to long
-                       Long countryId = Long.parseLong(submittedValue);
+                       final Long countryId = Long.parseLong(submittedValue);
 
                        // Category id should not be below 1
                        assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N