[jboss-cvs] JBossAS SVN: r101718 - in projects/ejb-book/trunk/ch06-filetransfer: src/main/java/org/jboss/ejb3/examples/ch06/filetransfer and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 2 10:35:28 EST 2010


Author: ALRubinger
Date: 2010-03-02 10:35:26 -0500 (Tue, 02 Mar 2010)
New Revision: 101718

Added:
   projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java
   projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/ftpusers.properties
Removed:
   projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java
   projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpserver-jboss-beans.xml
   projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
   projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties
Modified:
   projects/ejb-book/trunk/ch06-filetransfer/pom.xml
   projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java
Log:
[EJBBOOK-20] SFSB Examples to use Arquillian test harness

Modified: projects/ejb-book/trunk/ch06-filetransfer/pom.xml
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/pom.xml	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/pom.xml	2010-03-02 15:35:26 UTC (rev 101718)
@@ -95,6 +95,16 @@
       <version>${version.org.slf4j_slf4j.jcl}</version>
       <scope>test</scope>
     </dependency>
+    
+    <!-- Arquillian Test Harness -->
+    <dependency>
+      <groupId>org.jboss.arquillian</groupId>
+      <artifactId>arquillian-container-openejb</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.arquillian</groupId>
+      <artifactId>arquillian-junit</artifactId>
+    </dependency>
 
   </dependencies>
 
@@ -104,6 +114,9 @@
 
       <!-- Declare the "Integration Test" Profile -->
       <id>it</id>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
 
       <build>
 
@@ -124,10 +137,6 @@
                 </goals>
                 <configuration>
                   <skip>false</skip>
-                  <!-- Include jbossall-client.jar on the CP -->
-                  <additionalClasspathElements>
-                    <additionalClasspathElement>${jboss.home}/client/jbossall-client.jar</additionalClasspathElement>
-                  </additionalClasspathElements>
                   <redirectTestOutputToFile>true</redirectTestOutputToFile>
                   <printSummary>true</printSummary>
                   <forkMode>always</forkMode>
@@ -139,79 +148,6 @@
             </executions>
           </plugin>
 
-          <!--
-
-            Define the JBossAS Maven Control Plugin
-          -->
-          <plugin>
-
-            <groupId>org.jboss.maven.plugins.jbossas</groupId>
-            <artifactId>maven-jboss-as-control-plugin</artifactId>
-            <version>0.1.2-SNAPSHOT</version>
-
-            <!-- Executions -->
-            <executions>
-
-              <!--
-
-                Start JBossAS with our new configuration
-              -->
-              <execution>
-                <id>start-jbossas</id>
-                <goals>
-                  <goal>start</goal>
-                </goals>
-                <phase>pre-integration-test</phase>
-                <configuration>
-                  <jvmArgs>
-                    <jvmArg>-Xmx512M</jvmArg>
-                  </jvmArgs>
-                  <serverConfigName>default</serverConfigName>
-                  <jboss.test.run>true</jboss.test.run>
-                </configuration>
-              </execution>
-
-              <!--
-                Deploy into our custom JBossAS Configuration
-              -->
-              <execution>
-                <id>deploy-test-slsb</id>
-                <goals>
-                  <goal>deploy</goal>
-                </goals>
-                <phase>pre-integration-test</phase>
-                <configuration>
-                  <serverConfigName>default</serverConfigName>
-                  <files>${project.build.directory}/lib/ftplet-api.jar,
-                    ${project.build.directory}/lib/ftpserver-core.jar,
-                    ${project.build.directory}/lib/mina-core.jar,
-                    ${project.build.directory}/lib/slf4j-api.jar,
-                    ${project.build.directory}/lib/commons-net.jar,
-                    ${project.build.directory}/${project.build.finalName}.${project.packaging}</files>
-                  <jboss.test.run>true</jboss.test.run>
-                </configuration>
-              </execution>
-
-              <!--
-
-                Stop JBossAS
-              -->
-              <execution>
-                <id>stop-jbossas</id>
-                <goals>
-                  <goal>stop</goal>
-                </goals>
-                <phase>post-integration-test</phase>
-                <configuration>
-                  <serverConfigName>default</serverConfigName>
-                  <jboss.test.run>true</jboss.test.run>
-                </configuration>
-              </execution>
-
-            </executions>
-
-          </plugin>
-
         </plugins>
 
       </build>

Deleted: projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java	2010-03-02 15:35:26 UTC (rev 101718)
@@ -1,306 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
-  *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.examples.ch06.filetransfer;
-
-import java.net.URL;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-
-import org.apache.ftpserver.FtpServer;
-import org.apache.ftpserver.FtpServerFactory;
-import org.apache.ftpserver.ftplet.FtpException;
-import org.apache.ftpserver.ftplet.UserManager;
-import org.apache.ftpserver.listener.ListenerFactory;
-import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
-import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
-import org.jboss.logging.Logger;
-
-/**
- * FtpServerPojo
- * 
- * MC Bean (POJO) Responsible for starting/stopping 
- * the Embedded FTP Server.
- * 
- * It's the file "ftpserver-jboss-beans.xml" which will
- * install this into JBoss MicroContainer and invoke 
- * the appropriate lifecycle callbacks.  This should
- * be considered part of the test execution environment
- * and is not really part of the SFSB examples themselves.
- * The SFSBs for the examples are a client of the FTP server
- * started by this simple bean.
- * 
- * Thread-safe.  Lifecycle operations are synchronized on 
- * "this".
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public final class FtpServerPojo
-{
-
-   //-------------------------------------------------------------------------------------||
-   // Class Members ----------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Logger
-    */
-   private static final Logger log = Logger.getLogger(FtpServerPojo.class);
-
-   /**
-    * Name of the Server's default listener
-    */
-   private static final String LISTENER_NAME_DEFAULT = "default";
-
-   //-------------------------------------------------------------------------------------||
-   // Instance Members -------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Port to which the FTP Server will bind.
-    * 
-    * Synchronized on "this", volatile so
-    * we don't block for read-only access
-    */
-   private volatile int bindPort;
-
-   /**
-    * The underlying server.  Must not be exported.
-    */
-   private FtpServer server;
-
-   /**
-    * The name of the users/password configuration filename.
-    * 
-    * Synchronized on "this", volatile so
-    * we don't block for read-only access
-    */
-   private volatile String usersConfigFileName;
-
-   //-------------------------------------------------------------------------------------||
-   // Lifecycle Methods ------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Creates and initializes the underlying server.  Should be 
-    * called along lifecycle when this POJO is created.
-    * 
-    * Synchronized on "this" so we block on all lifecycle events
-    * 
-    * @throws IllegalStateException If the properties for the server have not
-    *       been properly initialized
-    */
-   public synchronized void initializeServer() throws IllegalStateException
-   {
-      // Extract properties
-      final int bindPort = this.getBindPort();
-
-      /*
-       * Precondition checks
-       */
-
-      if (bindPort <= 0)
-      {
-         throw new IllegalStateException("Property for bind port has not been set to a valid value above 0.");
-      }
-
-      // Initialize
-      final FtpServerFactory serverFactory = new FtpServerFactory();
-      final ListenerFactory factory = new ListenerFactory();
-
-      // Set properties
-      log.debug("Using FTP bind port: " + bindPort);
-      factory.setPort(bindPort);
-
-      // Add default listener to the server factory
-      serverFactory.addListener(LISTENER_NAME_DEFAULT, factory.createListener());
-
-      // Get the current CL
-      final ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
-      {
-         @Override
-         public ClassLoader run()
-         {
-            return Thread.currentThread().getContextClassLoader();
-         }
-      });
-
-      // Load the properties file to get its URI
-      final String usersConfigFileName = this.getUsersConfigFileName();
-      log.info("Using users configuration file: " + usersConfigFileName);
-      final URL usersConfigUrl = tccl.getResource(usersConfigFileName);
-      if (usersConfigUrl == null)
-      {
-         throw new RuntimeException("Could not find specified users configuration file upon the classpath: "
-               + usersConfigFileName);
-      }
-
-      // Configure the user auth mechanism
-      final PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
-      userManagerFactory.setUrl(usersConfigUrl);
-      userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
-      final UserManager userManager = userManagerFactory.createUserManager();
-      serverFactory.setUserManager(userManager);
-
-      // Create the server
-      final FtpServer server = serverFactory.createServer();
-      this.setServer(server);
-      log.info("Created FTP Server: " + server);
-   }
-
-   /**
-    * Starts the server.  Should be called along lifecycle when this
-    * POJO is installed (ie. @Start)
-    * 
-    * Synchronized on "this" so we block on all lifecycle events
-    * 
-    * @throws IllegalStateException If the server has not been initialized or
-    *       if the server has already been started
-    * @throws FtpException If there was an error in starting the server
-    */
-   public synchronized void startServer() throws IllegalStateException, FtpException
-   {
-      // Get the server
-      final FtpServer server = this.getServer();
-
-      /*
-       * Precondition checks
-       */
-
-      // Ensure initialized
-      if (server == null)
-      {
-         throw new IllegalStateException("The server has not yet been initialized");
-      }
-
-      // Ensure not already running or in some other state
-      if (!server.isStopped())
-      {
-         throw new IllegalStateException("Server cannot be started if it is not currently stopped");
-      }
-
-      // Start
-      log.debug("Starting the FTP Server: " + server);
-      server.start();
-      log.info("FTP Server Started: " + server);
-   }
-
-   /**
-    * Stops the server.  Should be called along lifecycle when this
-    * POJO is uninstalled.
-    * 
-    * @throws IllegalStateException If the server is already stopped or the server is
-    *       not initialized 
-    * @throws FtpException
-    */
-   public synchronized void stopServer() throws IllegalStateException
-   {
-      // Get the server
-      final FtpServer server = this.getServer();
-
-      /*
-       * Precondition checks
-       */
-
-      // Ensure initialized
-      if (server == null)
-      {
-         throw new IllegalStateException("The server has not yet been initialized");
-      }
-
-      // Ensure not already running or in some other state
-      if (server.isStopped())
-      {
-         throw new IllegalStateException("Server cannot be stopped if it's already stopped");
-      }
-
-      // Stop
-      log.debug("Stopping the FTP Server: " + server);
-      server.stop();
-      log.info("FTP Server stopped: " + server);
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Accessors / Mutators ---------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Obtains the port to which we'll bind
-    */
-   public int getBindPort()
-   {
-      return bindPort;
-   }
-
-   /**
-    * Sets the port to which we'll bind.  Synchronized on "this"
-    * so we block until lifecycle operations are complete.
-    * 
-    * @param bindPort
-    */
-   public synchronized void setBindPort(final int bindPort)
-   {
-      this.bindPort = bindPort;
-   }
-
-   /**
-    * Obtains the underlying FTP Server
-    * 
-    * @return
-    */
-   protected FtpServer getServer()
-   {
-      return server;
-   }
-
-   /**
-    * Sets the underlying FTP Server.  Synchronized on 
-    * "this" so we block until lifecycle operations are complete.
-    * 
-    * @param server
-    */
-   private synchronized void setServer(final FtpServer server)
-   {
-      this.server = server;
-   }
-
-   /**
-    * Obtains the name of the users configuration file
-    * 
-    * @return the usersConfigFileName
-    */
-   public String getUsersConfigFileName()
-   {
-      return usersConfigFileName;
-   }
-
-   /**
-    * Sets the name of the users configuration file.  Synchronized on "this"
-    * so we block until lifecycle operations are complete.
-    * 
-    * @param usersConfigFileName the usersConfigFileName to set
-    */
-   public synchronized void setUsersConfigFileName(final String usersConfigFileName)
-   {
-      this.usersConfigFileName = usersConfigFileName;
-   }
-}

Deleted: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpserver-jboss-beans.xml
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpserver-jboss-beans.xml	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpserver-jboss-beans.xml	2010-03-02 15:35:26 UTC (rev 101718)
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<deployment xmlns="urn:jboss:bean-deployer:2.0">
-
-  <!-- 
-  
-  Define the POJO which will Embed the FTP Server 
-  
-  This FTP Server will be used for our SFSB Examples
-  
-  -->
-  
-  <bean name="org.jboss.ejb3.examples.ch06.filetransfer.FtpServer"
-    class="org.jboss.ejb3.examples.ch06.filetransfer.FtpServerPojo">
-    
-    <!-- Define configurable properties -->
-    <property name="bindPort">12345</property>
-    <property name="usersConfigFileName">ftpusers.properties</property>
-    
-    <!-- Define callbacks to be invoked alongside MC Lifecycle -->
-    <install method="initializeServer" state="Configured" />
-    <install method="startServer" state="Started "/>
-    <uninstall method="stopServer" />
-    
-  </bean>
-  
-</deployment>
\ No newline at end of file

Deleted: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties	2010-03-02 15:35:26 UTC (rev 101718)
@@ -1,6 +0,0 @@
-# Users / Passwords file
-ftpserver.user.user.idletime=0
-ftpserver.user.user.userpassword=password
-ftpserver.user.user.homedirectory=/
-ftpserver.user.user.writepermission=true
-ftpserver.user.user.enableflag=true
\ No newline at end of file

Modified: projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java	2010-03-02 15:35:26 UTC (rev 101718)
@@ -23,17 +23,21 @@
 
 import java.io.File;
 
+import javax.ejb.EJB;
 import javax.ejb.NoSuchEJBException;
-import javax.naming.Context;
-import javax.naming.InitialContext;
 
 import junit.framework.TestCase;
 
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.logging.Logger;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.junit.After;
-import org.junit.Before;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * FileTransferIntegrationTestCase
@@ -49,6 +53,7 @@
  * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
  * @version $Revision: $
  */
+ at RunWith(Arquillian.class)
 public class FileTransferIntegrationTestCase extends FileTransferTestCaseBase
 {
 
@@ -62,16 +67,33 @@
    private static final Logger log = Logger.getLogger(FileTransferIntegrationTestCase.class);
 
    /**
-    * JNDI Name to which the FileTransferEJB is bound
+    * Name of the configuration file for the FTP server users
     */
-   //TODO Use Global JNDI Name (not yet available in JBoss EJB3)
-   private static final String JNDI_NAME_FILETRANSFER_EJB = FileTransferBean.EJB_NAME + "/remote";
+   private static final String FTP_SERVER_USERS_CONFIG_FILENAME = "ftpusers.properties";
 
    /**
-    * Naming context used for lookups
+    * Port to which the FTP server should bind
     */
-   private static Context namingContext;
+   private static final int FTP_SERVER_BIND_PORT = 12345;
 
+   /**
+    * The FTP Server
+    */
+   private static FtpServerPojo ftpServer;
+
+   /**
+    * The Deployment
+    * @return
+    */
+   @Deployment
+   public static JavaArchive createDeployment()
+   {
+      final JavaArchive archive = Archives.create("ftpclient.jar", JavaArchive.class).addPackage(
+            FileTransferBean.class.getPackage());
+      log.info(archive.toString(true));
+      return archive;
+   }
+
    //-------------------------------------------------------------------------------------||
    // Instance Members -------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -79,34 +101,46 @@
    /**
     * Our view of the EJB, remote business interface type of the Proxy
     */
-   private FileTransferRemoteBusiness client;
+   @EJB
+   private FileTransferRemoteBusiness client1;
 
+   /**
+    * Another FTP Client Session
+    */
+   @EJB
+   private FileTransferRemoteBusiness client2;
+
    //-------------------------------------------------------------------------------------||
    // Lifecycle --------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
    /**
-    * Creates and sets the JNDI Naming Context used to look up SFSB Proxies
+    * Creates and starts the FTP Server
     */
    @BeforeClass
-   public static void createNamingContext() throws Exception
+   public static void startFtpServer() throws Exception
    {
       // Create
-      final Context context = new InitialContext(); // Properties from ClassPath jndi.properties
+      final FtpServerPojo server = new FtpServerPojo();
 
-      // Log and set
-      log.info("Created JNDI Context: " + context);
-      namingContext = context;
+      // Configure
+      server.setUsersConfigFileName(FTP_SERVER_USERS_CONFIG_FILENAME);
+      server.setBindPort(FTP_SERVER_BIND_PORT);
+
+      // Start and set
+      server.initializeServer();
+      server.startServer();
+      ftpServer = server;
    }
 
    /**
-    * Obtains and sets the FTP Client SFSB Proxy
+    * Stops the FTP Server
+    * @throws Exception
     */
-   @Before
-   public void obtainClient() throws Exception
+   @AfterClass
+   public static void stopFtpServer() throws Exception
    {
-      // Set
-      client = this.createNewSession();
+      ftpServer.stopServer();
    }
 
    /**
@@ -114,12 +148,12 @@
     * and resets
     */
    @After
-   public void endClientSession() throws Exception
+   public void endClientSessions() throws Exception
    {
-      // End the session
+      // End the session for client 1
       try
       {
-         client.endSession();
+         client1.endSession();
       }
       // If we've already been ended
       catch (final NoSuchEJBException nsee)
@@ -127,8 +161,16 @@
          // Ignore
       }
 
-      // Clear
-      client = null;
+      // End the session for client 2
+      try
+      {
+         client2.endSession();
+      }
+      // If we've already been ended
+      catch (final NoSuchEJBException nsee)
+      {
+         // Ignore
+      }
    }
 
    //-------------------------------------------------------------------------------------||
@@ -149,8 +191,8 @@
       // Get the existing client as made from the test lifecycle
       final FileTransferRemoteBusiness session1 = this.getClient();
 
-      // Make a new session and use that as another client
-      final FileTransferRemoteBusiness session2 = this.createNewSession();
+      // Use another client
+      final FileTransferRemoteBusiness session2 = this.client2;
 
       // cd into a home directory for each
       final String ftpHome = getFtpHome().getAbsolutePath();
@@ -231,25 +273,7 @@
    @Override
    protected FileTransferRemoteBusiness getClient()
    {
-      return this.client;
+      return this.client1;
    }
 
-   //-------------------------------------------------------------------------------------||
-   // Internal Helper Methods ------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Obtains the SFSB Proxy from JNDI, creating a new user session
-    */
-   private FileTransferRemoteBusiness createNewSession() throws Exception
-   {
-      // Look up in JNDI
-      final String jndiName = JNDI_NAME_FILETRANSFER_EJB;
-      final Object proxy = namingContext.lookup(jndiName);
-      log.info("Obtained FTP EJB Proxy from JNDI at \"" + jndiName + "\" : " + proxy);
-
-      // Return
-      return (FileTransferRemoteBusiness) proxy;
-   }
-
 }

Copied: projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java (from rev 101715, projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java)
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java	2010-03-02 15:35:26 UTC (rev 101718)
@@ -0,0 +1,288 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch06.filetransfer;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.jboss.logging.Logger;
+
+/**
+ * FtpServerPojo
+ * 
+ * POJO Responsible for starting/stopping 
+ * the Embedded FTP Server.
+ * 
+ * This should be considered part of the test execution environment
+ * and is not really part of the SFSB examples themselves.
+ * The SFSBs for the examples are a client of the FTP server
+ * started by this simple bean.
+ * 
+ * Not thread-safe.  Intended to be used in single-Threaded environments
+ * (or perform your own external synchronization).
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public final class FtpServerPojo
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(FtpServerPojo.class);
+
+   /**
+    * Name of the Server's default listener
+    */
+   private static final String LISTENER_NAME_DEFAULT = "default";
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Port to which the FTP Server will bind.
+    */
+   private int bindPort;
+
+   /**
+    * The underlying server.  Must not be exported.
+    */
+   private FtpServer server;
+
+   /**
+    * The name of the users/password configuration filename.
+    */
+   private String usersConfigFileName;
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle Methods ------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates and initializes the underlying server.  Should be 
+    * called along lifecycle when this POJO is created.
+    * 
+    * @throws IllegalStateException If the properties for the server have not
+    *       been properly initialized
+    */
+   public void initializeServer() throws IllegalStateException
+   {
+      // Extract properties
+      final int bindPort = this.getBindPort();
+
+      /*
+       * Precondition checks
+       */
+
+      if (bindPort <= 0)
+      {
+         throw new IllegalStateException("Property for bind port has not been set to a valid value above 0.");
+      }
+
+      // Initialize
+      final FtpServerFactory serverFactory = new FtpServerFactory();
+      final ListenerFactory factory = new ListenerFactory();
+
+      // Set properties
+      log.debug("Using FTP bind port: " + bindPort);
+      factory.setPort(bindPort);
+
+      // Add default listener to the server factory
+      serverFactory.addListener(LISTENER_NAME_DEFAULT, factory.createListener());
+
+      // Get the current CL
+      final ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+      {
+         @Override
+         public ClassLoader run()
+         {
+            return Thread.currentThread().getContextClassLoader();
+         }
+      });
+
+      // Load the properties file to get its URI
+      final String usersConfigFileName = this.getUsersConfigFileName();
+      log.info("Using users configuration file: " + usersConfigFileName);
+      final URL usersConfigUrl = tccl.getResource(usersConfigFileName);
+      if (usersConfigUrl == null)
+      {
+         throw new RuntimeException("Could not find specified users configuration file upon the classpath: "
+               + usersConfigFileName);
+      }
+
+      // Configure the user auth mechanism
+      final PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
+      userManagerFactory.setUrl(usersConfigUrl);
+      userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
+      final UserManager userManager = userManagerFactory.createUserManager();
+      serverFactory.setUserManager(userManager);
+
+      // Create the server
+      final FtpServer server = serverFactory.createServer();
+      this.setServer(server);
+      log.info("Created FTP Server: " + server);
+   }
+
+   /**
+    * Starts the server.
+    * 
+    * @throws IllegalStateException If the server has not been initialized or
+    *       if the server has already been started
+    * @throws FtpException If there was an error in starting the server
+    */
+   public void startServer() throws IllegalStateException, FtpException
+   {
+      // Get the server
+      final FtpServer server = this.getServer();
+
+      /*
+       * Precondition checks
+       */
+
+      // Ensure initialized
+      if (server == null)
+      {
+         throw new IllegalStateException("The server has not yet been initialized");
+      }
+
+      // Ensure not already running or in some other state
+      if (!server.isStopped())
+      {
+         throw new IllegalStateException("Server cannot be started if it is not currently stopped");
+      }
+
+      // Start
+      log.debug("Starting the FTP Server: " + server);
+      server.start();
+      log.info("FTP Server Started: " + server);
+   }
+
+   /**
+    * Stops the server. 
+    * 
+    * @throws IllegalStateException If the server is already stopped or the server is
+    *       not initialized 
+    * @throws FtpException
+    */
+   public void stopServer() throws IllegalStateException
+   {
+      // Get the server
+      final FtpServer server = this.getServer();
+
+      /*
+       * Precondition checks
+       */
+
+      // Ensure initialized
+      if (server == null)
+      {
+         throw new IllegalStateException("The server has not yet been initialized");
+      }
+
+      // Ensure not already running or in some other state
+      if (server.isStopped())
+      {
+         throw new IllegalStateException("Server cannot be stopped if it's already stopped");
+      }
+
+      // Stop
+      log.debug("Stopping the FTP Server: " + server);
+      server.stop();
+      log.info("FTP Server stopped: " + server);
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Accessors / Mutators ---------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains the port to which we'll bind
+    */
+   public int getBindPort()
+   {
+      return bindPort;
+   }
+
+   /**
+    * Sets the port to which we'll bind.
+    * 
+    * @param bindPort
+    */
+   public void setBindPort(final int bindPort)
+   {
+      this.bindPort = bindPort;
+   }
+
+   /**
+    * Obtains the underlying FTP Server
+    * 
+    * @return
+    */
+   protected FtpServer getServer()
+   {
+      return server;
+   }
+
+   /**
+    * Sets the underlying FTP Server. 
+    * 
+    * @param server
+    */
+   private void setServer(final FtpServer server)
+   {
+      this.server = server;
+   }
+
+   /**
+    * Obtains the name of the users configuration file
+    * 
+    * @return the usersConfigFileName
+    */
+   public String getUsersConfigFileName()
+   {
+      return usersConfigFileName;
+   }
+
+   /**
+    * Sets the name of the users configuration file.
+    * 
+    * @param usersConfigFileName the usersConfigFileName to set
+    */
+   public void setUsersConfigFileName(final String usersConfigFileName)
+   {
+      this.usersConfigFileName = usersConfigFileName;
+   }
+}

Copied: projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/ftpusers.properties (from rev 101715, projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties)
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/ftpusers.properties	                        (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/ftpusers.properties	2010-03-02 15:35:26 UTC (rev 101718)
@@ -0,0 +1,6 @@
+# Users / Passwords file
+ftpserver.user.user.idletime=0
+ftpserver.user.user.userpassword=password
+ftpserver.user.user.homedirectory=/
+ftpserver.user.user.writepermission=true
+ftpserver.user.user.enableflag=true
\ No newline at end of file


Property changes on: projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/ftpusers.properties
___________________________________________________________________
Name: svn:mergeinfo
   + 

Deleted: projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties	2010-03-02 15:30:09 UTC (rev 101717)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties	2010-03-02 15:35:26 UTC (rev 101718)
@@ -1,4 +0,0 @@
-# JNDI Properties for Remote interaction with JBoss Application Server Naming Service 
-java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
-java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
-java.naming.provider.url=jnp://localhost:1099
\ No newline at end of file




More information about the jboss-cvs-commits mailing list