Return-Path: X-Original-To: apmail-myfaces-users-archive@www.apache.org Delivered-To: apmail-myfaces-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E5818104B7 for ; Sat, 13 Apr 2013 11:41:10 +0000 (UTC) Received: (qmail 65411 invoked by uid 500); 13 Apr 2013 11:41:10 -0000 Delivered-To: apmail-myfaces-users-archive@myfaces.apache.org Received: (qmail 64766 invoked by uid 500); 13 Apr 2013 11:41:06 -0000 Mailing-List: contact users-help@myfaces.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "MyFaces Discussion" Delivered-To: mailing list users@myfaces.apache.org Received: (qmail 64707 invoked by uid 99); 13 Apr 2013 11:41:04 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 13 Apr 2013 11:41:04 +0000 X-ASF-Spam-Status: No, hits=1.7 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of smithh032772@gmail.com designates 74.125.82.172 as permitted sender) Received: from [74.125.82.172] (HELO mail-we0-f172.google.com) (74.125.82.172) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 13 Apr 2013 11:40:57 +0000 Received: by mail-we0-f172.google.com with SMTP id r3so2666820wey.17 for ; Sat, 13 Apr 2013 04:40:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=vI2ZO0jZCIOdQWh5qFC6ejCL3KiO3BB5dHLaAuioFKE=; b=PIFUaLIzf1vQ/YCHbLLeAr/yuX7CbjUSXg8B9qagBzZOibPK1ELjerqcWd5JdnqXEk mUpzKfuQD2zc0KnosGFvZqnx/ZYciI378bEPmyHVU5+jT1J83ndmisCF1dKfbInLtAXS 7gD3IiD71b/rr8UZ9YfymsGoXA7qyO2SuZaD14g7SZrTnah9L7WOrzqIbjf3cX9WMajk t648nL7j4lx8/j+6tGRfQPd3ewRgmY3PvpqVi46Ow0FZoNgNpSFEXOJN/t8R/0Fi+ohI XZviJf6m5L8ibQGKbQnY9haxVaN4M756/pU1H8i4D9F/P/mNv0RwMQUMnbMkORWAEpDF uwWg== MIME-Version: 1.0 X-Received: by 10.194.81.40 with SMTP id w8mr22598556wjx.14.1365853237302; Sat, 13 Apr 2013 04:40:37 -0700 (PDT) Received: by 10.227.25.194 with HTTP; Sat, 13 Apr 2013 04:40:37 -0700 (PDT) Date: Sat, 13 Apr 2013 07:40:37 -0400 Message-ID: Subject: @Singleton @Lock(READ) to resolve, Could not instantiate converter ... From: "Howard W. Smith, Jr." To: MyFaces Discussion Content-Type: multipart/alternative; boundary=047d7bb04364065cb804da3c7d38 X-Virus-Checked: Checked by ClamAV on apache.org --047d7bb04364065cb804da3c7d38 Content-Type: text/plain; charset=ISO-8859-1 In an effort to remove 'static' declarations throughout my app (to help JVM's GC), I removed 'static' from the definition of the Converter class below. So that resulted in the infamous error below: Apr 13, 2013 4:10:38 AM org.apache.myfaces.application.ApplicationImpl internalCreateConverter SEVERE: Could not instantiate converter jsf.CustomerController$CustomerControllerConverter java.lang.InstantiationException: jsf.CustomerController$CustomerControllerConverter at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.apache.myfaces.application.ApplicationImpl.internalCreateConverter(ApplicationImpl.java:1626) at org.apache.myfaces.application.ApplicationImpl.createConverter(ApplicationImpl.java:1545) at javax.faces.application.ApplicationWrapper.createConverter(ApplicationWrapper.java:158) I assume 'static' is necessary, because only one copy of the class is created per application. Correct? So, can I define the class as a @Singleton @Lock(READ) to resolve the issue? Per NetBeans generated JSF controller/bean code, the Converter is usually defined in the same .java file as the controller or @ManagedBean. Honestly, I do 'not' want to use addConverter() or converterId="..." in the xhtml. I prefer to use @FacesConverter, since this has been working for me throughout the app. package jsf; import jpa.entities.Customer; import jpa.session.CustomerFacade; import java.io.Serializable; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; @ManagedBean(name = "customerController") @RequestScoped public class CustomerController implements Serializable { @EJB private jpa.session.CustomerFacade ejbFacade; public CustomerController() { } @FacesConverter(forClass = Customer.class) public class CustomerControllerConverter implements Converter { public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { if (value == null || value.length() == 0) { return null; } /* * 2012-07-10 when user enters invalid/incomplete value (e.g. "irene", see below) in AutoComplete * WARNING: For input string: "irene" java.lang.NumberFormatException: For input string: "irene" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.valueOf(Integer.java:582) at jsf.pointOfContact.pf_PointOfContactController$PointOfContactControllerConverter.getKey(pf_PointOfContactController.java:1625) at jsf.pointOfContact.pf_PointOfContactController$PointOfContactControllerConverter.getAsObject(pf_PointOfContactController.java:1620) at org.primefaces.component.autocomplete.AutoCompleteRenderer.getConvertedValue(AutoCompleteRenderer.java:529) at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) at javax.faces.component.UIInput.validate(UIInput.java:960) * */ try { Integer test = getKey(value); } catch (java.lang.NumberFormatException e) { return null; } CustomerController controller = (CustomerController) facesContext.getApplication().getELResolver(). getValue(facesContext.getELContext(), null, "customerController"); return controller.ejbFacade.find(getKey(value)); } java.lang.Integer getKey(String value) { java.lang.Integer key; key = Integer.valueOf(value); return key; } String getStringKey(java.lang.Integer value) { StringBuffer sb = new StringBuffer(); sb.append(value); return sb.toString(); } public String getAsString(FacesContext facesContext, UIComponent component, Object object) { if (object == null) { return null; } if (object instanceof Customer) { Customer o = (Customer) object; return getStringKey(o.getCustomerId()); } else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Customer.class.getName()); } } } } Thanks, Howard --047d7bb04364065cb804da3c7d38--