need help with java extensions

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

digiplayer
Posts: 7
Joined: 10 Apr 2009, 16:48

need help with java extensions

Postby digiplayer » 11 Apr 2009, 13:25

Hello everyone,

I have been trying for the last two days to get a java extenstion set up and working and I have not gotten any closer. I could really use some advice.

I created a new project called sfs in eclipse and the default package is ext. Within ext I created a class called userLogin which contains the following.

Code: Select all

package ext;


import java.nio.channels.SocketChannel;
import java.util.*;
import org.json.JSONObject;
import it.gotoandplay.smartfoxserver.db.*;
import it.gotoandplay.smartfoxserver.data.*;
import it.gotoandplay.smartfoxserver.exceptions.*;
import it.gotoandplay.smartfoxserver.extensions.*;
import it.gotoandplay.smartfoxserver.lib.ActionscriptObject;
import it.gotoandplay.smartfoxserver.events.InternalEventObject;
import it.gotoandplay.smartfoxserver.lib.*;


public class userLogin extends AbstractExtension
{
   ExtensionHelper helper;
   Zone currentZone;
   DbManager dbManager;
   ActionscriptObject res;
   
   public void init()
   {
      helper = ExtensionHelper.instance();
      currentZone = helper.getZone(this.getOwnerZone());
      dbManager = currentZone.dbManager;
   }

   public void destroy()
   {
      trace("Extension destroyed");
   }

   public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom)
   {
             
   }
   
   public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom)
   {
      // Your code here, handles JSON-based requests
   }
   
   public void handleRequest(String cmd, String params[], User u, int fromRoom)
   {
      // Your code here, handles String-based requests
   }

   public void handleInternalEvent(InternalEventObject evt)
   {
      if (evt.getEventName().equals(InternalEventObject.EVENT_LOGIN))
      {
            boolean ok = false;
            User newUser = null;
            
            // Prepare a response object for the client
            ActionscriptObject res = new ActionscriptObject();
            
            // get the user nickname and password
            String nick = evt.getParam("nick");
            String pass = evt.getParam("pass");
            
            // get the user socket channel
            SocketChannel chan = (SocketChannel) evt.getObject("chan");
            
            // validate user name
            ok = checkCredentials(nick, pass);
            
            if (ok)
            {
               try
               {
                  // Attempt to login the user in the system
                  newUser = helper.canLogin(nick, pass, chan, currentZone.toString());
                  
                  res.put("_cmd", "logOK");
                  res.put("id", String.valueOf(newUser.getUserId()));
                  res.put("name", newUser.getName());

                  ok = true;
               }
               
               // An exception occurred while logging the user in
               catch (LoginException le)
               {
                  this.trace("Could not login user: " + nick);

                  res.put("_cmd", "logKO");
                  res.put("err", le.getMessage());
               }
            }
            
            // Invalid credentials
            else
            {
               res.put("_cmd", "logKO");
               res.put("err", "Sorry, invalid credentials.");
            }
            
            // Prepare the list of recipients, in this case we only one.
            LinkedList<SocketChannel> ll = new LinkedList<SocketChannel>();
            ll.add(chan);

            // Send login response
            sendResponse(res, -1, null, ll);   

            // Send room list
            if (ok)
               helper.sendRoomList(chan);
         }   
      
   }
   
   public Object handleInternalRequest(Object param)
   {
      return param;
      // (Optional) Your code here, handles an internal request
   }
   
   
   @SuppressWarnings("unchecked")
   boolean checkCredentials(String name, String pass)
   {
      boolean result = false;
      
      // Escape quotes on passed data
      name = SmartFoxLib.escapeQuotes(name);
      pass = SmartFoxLib.escapeQuotes(pass);
      
      // SQL statement to execute
      
      
      String sql = "SELECT id,username,pass FROM users WHERE" +
               " username='" + name + "'" +
               " AND pass='" + pass + "'";
      
      // Execute the SQL statement
      ArrayList queryRes = dbManager.executeQuery(sql);
         
      // If record was found exist...
      if (queryRes != null && queryRes.size() > 0)
      {
         result = true;
      }   
         
      return result;
   }
   
   
}

I then added the java server libraries to the server/lib directory and added them to the class path. below is my class path.


Code: Select all

@echo off
"..\jre\bin\java.exe" -cp "./;./sfsExtensions/;./javaExtensions/;lib/activation.jar;lib/commons-beanutils.jar;lib/commons-collections-3.2.jar;lib/commons-dbcp-1.2.1.jar;lib/commons-lang-2.3.jar;lib/commons-logging-1.1.jar;lib/commons-pool-1.2.jar;lib/concurrent.jar;lib/ezmorph-1.0.3.jar;lib/h2.jar;lib/js.jar;lib/json-lib-2.1-jdk15.jar;lib/json.jar;lib/jsr173_1.0_api.jar;lib/jysfs.jar;lib/jython.jar;lib/nanoxml-2.2.1.jar;lib/wrapper.jar;lib/xbean.jar;lib/javamail/imap.jar;lib/javamail/mailapi.jar;lib/javamail/pop3.jar;lib/javamail/smtp.jar;lib/jetty/jetty-6.1.3.jar;lib/jetty/jetty-util-6.1.3.jar;lib/jetty/jstl.jar;lib/jetty/multipartrequest.jar;lib/jetty/servlet-api-2.5-6.1.3.jar;lib/jetty/standard.jar;lib/jsp-2.1/commons-el-1.0.jar;lib/jsp-2.1/core-3.1.0.jar;lib/jsp-2.1/jsp-2.1.jar;lib/jsp-2.1/jsp-api-2.1.jar;lib/jsp-2.1/jstl.jar;lib/jsp-2.1/standard.jar;lib/json.jar;lib/nanoxml-2.2.1.jar;lib/sfs_java_api.jar;lib/xmlsocket.jar;lib/xSocket-1.2.1.jar" -Dfile.encoding=UTF-8 -Djava.util.logging.config.file=logging.properties it.gotoandplay.smartfoxserver.SmartFoxServer
@pause



I then created a jar file of my new project and extracted the file structure and place all the files in the server/javaExtensions folder.

I then added the entry to my config file like this

Code: Select all

<Zone name="simpleChat" uCountUpdate="true" buddyList="20" maxUsers="4000" customLogin="true">
         <Rooms>
            <Room name="The Hall" maxUsers="50" isPrivate="false" isTemp="false" autoJoin="true" uCountUpdate="true" />      
            <Room name="The Kitchen" maxUsers="50" isPrivate="false" isGame="false" isTemp="false" />
            <Room name="The Garden" maxUsers="50" isPrivate="false" isTemp="false" />
            <Room name="The Bathroom" maxUsers="50" isPrivate="false" isTemp="false" />
            <Room name="The Garage" maxUsers="50" isPrivate="false" isTemp="false" />
            <Room name="The Living Room" maxUsers="50" isPrivate="true" isTemp="false" pwd="test" />
         </Rooms>
         
         <Extensions>
            <extension name="json" className="jsonSample.as" type="script" />
            <extension name="userLogin" className="sfs.ext.userLogin" type="java" />
         </Extensions>
         
         <Moderators status="on">
            <Mod name="modName" pwd="modPass" />
         </Moderators>
      </Zone>


I have tried every thing that I can think of and I get the same error when loading the server every time. Below is the error.

Code: Select all

|::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::|
|                                                            |
|              ...:::  SmartFoxServer :::...                 |
|                Multiplayer Socket Server                   |
|                      version 1.6.2                         |
|                           ---                              |
|              (c) 2004 - 2007 gotoAndPlay()                 |
|                  www.smartfoxserver.com                    |
|                    www.gotoandplay.it                      |
|                                                            |
|::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::|

2009-04-11 10:42:34.968::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2009-04-11 10:42:35.109::WARN:  Deprecated configuration used for ./webserver/we
bapps
2009-04-11 10:42:35.140::INFO:  jetty-6.1.3
2009-04-11 10:42:35.218::INFO:  Extract jar:file:/C:/Program%20Files/SmartFoxSer
verPRO_1.6.2/Server/webserver/webapps/BlueBox.war!/ to C:\DOCUME~1\cjh\LOCALS~1\
Temp\Jetty_0_0_0_0_8080_BlueBox.war__BlueBox__-udbwk9\webapp
::::::::::: { BlueBox INITED } ::::::::::::
:                                         :
: Version 1.0.3 -- (c) 2007 gotoAndPlay() :
:                                         :
:::::::::::::::::::::::::::::::::::::::::::
2009-04-11 10:42:35.921::INFO:  Started SelectChannelConnector @ 0.0.0.0:8080
10:42:35.921 - [ INFO ] > Starting h2 engine...


--- [ System Info ] ------------------------------------------

System CPU(s): 2
VM Max memory: 66 MB

os.name: Windows XP
os.arch: x86
os.version: 5.1
java.version: 1.6.0_03
java.vendor: Sun Microsystems Inc.
java.vendor.url: http://java.sun.com/
java.vm.specification.version: 1.0
java.vm.version: 1.6.0_03-b05
java.vm.vendor: Sun Microsystems Inc.
java.vm.name: Java HotSpot(TM) Client VM


--- [ Network Cards ] -----------------------------------------

Card:MS TCP Loopback interface
 -> 127.0.0.1
Card:Broadcom 440x 10/100 Integrated Controller
 -> 174.100.25.212


--- [ Licence Loaded ] ------------------------------------

License type    -> PRO
License owner   -> --== Free Demo Licence ==--
Max clients     -> 20
Bluebox type    -> Demo Version

--- [ Zones & Rooms ] -------------------------------------

10:42:36.640 - [ INFO ] > Default Buddy List Persister <INITED>
Zone: simpleChat

        The Hall           (id: 1, max: 50, pass:N)
        The Kitchen        (id: 2, max: 50, pass:N)
        The Garden         (id: 3, max: 50, pass:N)
        The Bathroom       (id: 4, max: 50, pass:N)
        The Garage         (id: 5, max: 50, pass:N)
        The Living Room    (id: 6, max: 50, pass:Y)
10:42:37.078 - [ INFO ] > Zone Extension [ json ] created, for zone:simpleChat
[jsonSample.as]: JSON Example initialized
Exception in thread "SmartFoxServer" java.lang.NoClassDefFoundError: sfs/ext/use
rLogin (wrong name: ext/userLogin)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.loadExtens
ion(ExtensionHandler.java:108)
        at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.createExte
nsion(ExtensionHandler.java:207)
        at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.createExte
nsion(ExtensionHandler.java:189)
        at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.parse_Exte
nsions(ExtensionHandler.java:654)
        at it.gotoandplay.smartfoxserver.SmartFoxServer.setupZone(SmartFoxServer
.java:1997)
        at it.gotoandplay.smartfoxserver.lib.ConfigReader.parse_Zones(ConfigRead
er.java:736)
        at it.gotoandplay.smartfoxserver.lib.ConfigReader.readZoneConfig(ConfigR
eader.java:220)
        at it.gotoandplay.smartfoxserver.SmartFoxServer.initServerSocket(SmartFo
xServer.java:334)
        at it.gotoandplay.smartfoxserver.SmartFoxServer.run(SmartFoxServer.java:
588)



Can anyone see what I am doing wrong. Any suggestions will be helpfull.

Thanks.
draive
Posts: 51
Joined: 20 Dec 2008, 22:57

Postby draive » 11 Apr 2009, 14:10

I then created a jar file of my new project and extracted the file structure and place all the files in the server/javaExtensions folder.


You shouldnt have to create a jar of your project. You can set it to autobuild and just copy the .class files from your bin under your workstation to the sfs java extensions folder.
digiplayer
Posts: 7
Joined: 10 Apr 2009, 16:48

Postby digiplayer » 11 Apr 2009, 15:25

Thanks, all I had to do was create a folder called ext and place the class file in that directory.

The server loads now.

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 6 guests