10 Replies Latest reply: Sep 25, 2012 10:52 AM by Chris Laprun RSS

How to use factory method from a bean in jboss:pojo:7?

Zemian Deng Apprentice

Hi there,

 

In older version of jboss pojo, the docs says we can do this:

 

 <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
     <constructor factoryMethod="createBean">
         <factory bean="MyOtherFactory"/>
         <parameter>a string</parameter>
         <parameter>7</parameter>
     </constructor>
 </bean>

 

But this is no longer valid with latest "urn:jboss:pojo:7.0 jboss-pojo_7_0.xsd" schema. I noticed the "factoryMethod" has changed to "factory-method", but the "factory" element no longer support "bean" attr?

 

How would I do same config above in the new AS7?

 

Thanks,

Zemian

  • 1. Re: How to use factory method from a bean in jboss:pojo:7?
    Ales Justin Master

    This should still work (apart from the factoryMethod --> factory-method):

     

        private ConstructorConfig parseConstructor(final XMLExtendedStreamReader reader) throws XMLStreamException {

            final ConstructorConfig ctorConfig = new ConstructorConfig();

            final int count = reader.getAttributeCount();

            for (int i = 0; i < count; i++) {

                final Attribute attribute = Attribute.of(reader.getAttributeLocalName(i));

                final String attributeValue = reader.getAttributeValue(i);

     

     

                switch (attribute) {

                    case FACTORY_CLASS:

                        ctorConfig.setFactoryClass(attributeValue);

                        break;

                    case FACTORY_METHOD:

                        ctorConfig.setFactoryMethod(attributeValue);

                        break;

                    default:

                        throw unexpectedAttribute(reader, i);

                }

            }

            List<ValueConfig> parameters = new ArrayList<ValueConfig>();

            while (reader.hasNext()) {

                switch (reader.next()) {

                    case END_ELEMENT:

                        ctorConfig.setParameters(parameters.toArray(new ValueConfig[parameters.size()]));

                        return ctorConfig;

                    case START_ELEMENT:

                        switch (Element.of(reader.getLocalName())) {

                            case FACTORY:

                                ctorConfig.setFactory(parseFactory(reader));

                                break;

                            case PARAMETER:

                                ValueConfig p = parseParameter(reader);

                                p.setIndex(parameters.size());

                                parameters.add(p);

                                break;

                            default:

                                throw unexpectedElement(reader);

                        }

                }

            }

            throw unexpectedElement(reader);

        }

     

        private FactoryConfig parseFactory(final XMLExtendedStreamReader reader) throws XMLStreamException {

            final FactoryConfig factoryConfig = new FactoryConfig();

            final Set<Attribute> required = EnumSet.of(Attribute.BEAN);

            final int count = reader.getAttributeCount();

            for (int i = 0; i < count; i++) {

                final Attribute attribute = Attribute.of(reader.getAttributeLocalName(i));

                required.remove(attribute);

                final String attributeValue = reader.getAttributeValue(i);

     

     

                switch (attribute) {

                    case BEAN:

                        factoryConfig.setBean(attributeValue); /// <----------------- bean

                        break;

                    case STATE:

                        factoryConfig.setState(BeanState.valueOf(attributeValue.toUpperCase(Locale.ENGLISH)));

                        break;

                    default:

                        throw unexpectedAttribute(reader, i);

                }

            }

            if (required.isEmpty() == false) {

                throw missingRequired(reader, required);

            }

            while (reader.hasNext()) {

                switch (reader.next()) {

                    case END_ELEMENT:

                        return factoryConfig;

                }

            }

            throw unexpectedElement(reader);

        }

  • 2. Re: How to use factory method from a bean in jboss:pojo:7?
    Zemian Deng Apprentice

    Hi Ales,

     

    So is the xsd outdated or I read it wrong that it didn't have the "bean" attribute defined by the "factory" element? I was looking at https://github.com/jbossas/jboss-as/blob/master/build/src/main/resources/docs/schema/jboss-pojo_7_0.xsd

     

    Also, when I tried the following jboss-beans.xml, it won't deploy.

     

    <?xml version="1.0" encoding="UTF-8"?>
    <deployment xmlns="urn:jboss:pojo:7.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="urn:jboss:pojo:7.0 jboss-pojo_7_0.xsd">
              <bean name="schedulerFactory" class="timemachine.scheduler.SchedulerFactory">
                  <constructor>
                      <parameter>${user.home}/timemachine-scheduler.properties</parameter>
                        </constructor>
              </bean>
              <bean name="scheduler" class="timemachine.scheduler.Scheduler">
                  <constructor factory-method="createScheduler">
                                  <factory bean="schedulerFactory"/>
                        </constructor>
              </bean>
    </deployment>
    

     

    I get the follwing error:

     

    08:48:16,363 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."scheduler-jboss-pojo-1.0.0-SNAPSHOT.jar".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."scheduler-jboss-pojo-1.0.0-SNAPSHOT.jar".PARSE: Failed to process phase PARSE of deployment "scheduler-jboss-pojo-1.0.0-SNAPSHOT.jar"
              at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_05]
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_05]
              at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_05]
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS017052: Failed to parse POJO xml [ "/content/scheduler-jboss-pojo-1.0.0-SNAPSHOT.jar/META-INF/jboss-beans.xml" ]
              at org.jboss.as.pojo.KernelDeploymentParsingProcessor.parseDescriptor(KernelDeploymentParsingProcessor.java:156)
              at org.jboss.as.pojo.KernelDeploymentParsingProcessor.parseDescriptors(KernelDeploymentParsingProcessor.java:128)
              at org.jboss.as.pojo.KernelDeploymentParsingProcessor.deploy(KernelDeploymentParsingProcessor.java:83)
              at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
              ... 5 more
    

     

    Would you able to spot anything I amissed?

     

    Thanks,

    Zemian

     

    Added xsd notes.

  • 3. Re: How to use factory method from a bean in jboss:pojo:7?
    Ales Justin Master

    Can you debug and catch that exception on line 156 in KernelDeploymentParsingProcessor?

  • 4. Re: How to use factory method from a bean in jboss:pojo:7?
    David Yan Newbie

    Hi,

    I'm also using old deployment descriptors. 'bean' attribute is ok for <factory> element. But text content is not allowed as child of <parameter> element. You can try <parameter><value class="java.lang.String">${user.home}/timemachine-scheduler.properties</value></parameter>.

  • 5. Re: How to use factory method from a bean in jboss:pojo:7?
    Ales Justin Master

    But text content is not allowed as child of <parameter> element.

    Did we support this before?

  • 6. Re: How to use factory method from a bean in jboss:pojo:7?
    David Yan Newbie

    Yes, it can be directly added in <parameter> for previous JBoss

  • 7. Re: How to use factory method from a bean in jboss:pojo:7?
    Tomaz Cerar Master

    David,

     

    Ales has prepared fix for this and it is already avaliable in upstream. so if you take latest nightly build this should work.

     

     

    --

    tomaz

  • 8. Re: How to use factory method from a bean in jboss:pojo:7?
    Zemian Deng Apprentice

    Hi Tomaz+Ales,

     

    Sorry for the long delay. I did a build on latest code (rev : 886bb7e43374b5040f4edbe880a6c2163ec1a819) and my xml above worked without the error now! Thanks for the fix Ales.

     

    I am still newbie with Git searching, may I ask which revision did you fixed on?

     

    Thanks,

    Zemian

  • 10. Re: How to use factory method from a bean in jboss:pojo:7?
    Chris Laprun Master

    The XSD needs to be updated then since the parameter type doesn't allow such usage…