2011年5月18日 星期三

Linux上配置Blaze

連結資料:
BlazeDS來源網站連結參考
BlazeDS請於Adobe Open Source網站下載
BlazeDS下載首頁,可選擇一般檔案下載或原碼下載(SVN)。
BlazeDS一般檔案下載頁
BlazeDS Source Code下載頁
這篇文章將會使用一般檔案安裝。

安裝步驟:
Step1.配置JDK環境
請參考
    Ubuntu 10.04是預設是使用open java,如果要程式開發想使用Sun Java請參考install Sun JAVA。

    Step2.下載BlazeDS
    如圖:






     
     
     
    • 點擊後會進入Adobe下載頁面,輸入你在Adobe註冊的帳號與密碼。
    • 登入完畢後會出現BlazeDS的使用條款,點擊IAgree(我同意)。
    • 選擇Binary Distribution版本下載(這個版本沒有內含Tomcat,因為我們選擇自己安裝Tomcat)。 
         
     
    下載頁面也有列出BlazeDS的技術支援表:
    BlazeDS不支援Hibernate adapter,但有支援Spring
     
    在ubuntu使用firefox下載完後檔案會預設存放在home/Downloads裡面
     
    Step3.將BlazeDS解壓縮(解ZIP)
    • /~Downloads$ unzip blazeds-bin-4.0.0.14931.zip
    解開後得到blazeds.war

    Step4.將blazeds.war放置tomcat6中的webapps目錄下
    tomcat6預設上會將war解壓
    webapps是tomcat6預設的web資料夾位置
    • /~Downloads$ sudo mv blazeds.war /opt/tomcat6/webapps
    執行後將會得到一個blazeds資料夾

    Step5.修改blazeds名稱成為blazedsAMF

    • /opt/tomcat6/webapps$ sudo mv blazeds blazedsAMF
    Step6.將blazeds.war從webapps中刪除
    • /opt/tomcat6/webapps$ sudo rm blazeds.war

    Step7.利用Eclipse開啟java專案 (這個檔案沒有使用servlet)
    • 新增一專案,名稱remoteUT
    • 新增一packagecom.jt.remote
    • 新增一檔案,Hello.java
    結構如下:










    Step7.建立Hello.java程式內容
    package com.jt.remote;
    public class Hello
    {
     public Hello()
     {
      super();
     }
     public String getHello()
     {
      return "Hello World";
     }
     //test private
     private String getPackage()
     {
      return "com.jt.remote";
     }
     public String echoHello(String value)
     {
      return value + "Hello World";
     }
    }

    Step8.將remoteUT裡的src複製到blazedsAMF/WEB-INF/src底下
    • ~/workspace$ cp -a -r remoteUT/src/com /opt/tomcat6/webapps/blazedsAMF/WEB-INF/src/
    Step9.編譯Hello.java
    • ubuntu:/opt/tomcat6/webapps/blazedsAMF/WEB-INF/src$
      javac -d ../classes com/jt/remote/Hello.java

      編譯完成,可以在WEB-INF/classes中看見com/jt/remote/Hello.class
    Step10.配置remoting-config.xml
    • sudo gedit /opt/tomcat6/webapps/blazedsAMF/WEB-INF/flex/remoting-config.xml
    在<service>之間加入:

    <destination id="Remote_SayHello">
       <properties>
         <source>com.jt.remote.Hello</source>
       </properties>
    </destination>


    Step11.重新啟動Tomcat
    • /opt/tomcat6/bin$ ./startup.sh
    Step12.寫一個Flex來測試功能
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          minWidth="955" minHeight="600"
          applicationComplete="completeHandler(event)"
          >
     <s:layout>
      <s:HorizontalLayout/>
     </s:layout>
     <fx:Script>
      <![CDATA[
       import mx.events.FlexEvent;
       import mx.messaging.ChannelSet;
       import mx.messaging.channels.AMFChannel;
       import mx.rpc.events.FaultEvent;
       import mx.rpc.events.ResultEvent;
       import mx.rpc.remoting.Operation;
       import mx.rpc.remoting.RemoteObject;
     
       //Remote Operation
       private var _channelSet:ChannelSet;
       private var _amfChannel:AMFChannel;
     
       private var _getHelloRemoteObject:RemoteObject;
       private var _echoHelloRemoteObject:RemoteObject;
     
       private var _getHelloOperation:Operation;
       private var _echoHelloOperation:Operation;
     
       private var _getHelloData:Object;
       private var _echoHelloData:Object;  
       protected function completeHandler(event:FlexEvent):void
       {
        initRemoteData();
        initRemoteOpertion();    
        initBtnListener();
       }
     
       private function initRemoteData():void
       {
        _getHelloData  = createRemoteData("getHello");
        _echoHelloData = createRemoteData("echoHello");
       } 
       protected function createRemoteData(methodName:String):Object
       {  
        var data:Object = new Object(); 
        //blazeds的AMF Server Service服務位址
        data.uri = "http://192.168.2.103:8400/blazedsAMF/messagebroker/amf";            
        data.channelSetID = "my-amf";
        //服務程式來源path(由classes之後算起)
        data.source = "com.jt.remote.Hello";
        //操作method名稱
        data.name = methodName;   
        data.destination = "Remote_SayHello";
        return data;
       } 
     
       private function initRemoteOpertion():void
       {
        initChannelSet();
        initAMFChannel();
        addChannel();
        initRemoteObject();
        initOperation();
        afterInitOPeration();
       } 
       private function initChannelSet():void
       {  
        _channelSet = new ChannelSet();
       }
       private function initAMFChannel():void
       {
        _amfChannel = new AMFChannel(_getHelloData.channelSetID , _getHelloData.uri);
       }
       private function addChannel():void
       {
        _channelSet.addChannel(_amfChannel);
       }
       private function initRemoteObject():void
       {  
        _getHelloRemoteObject = new RemoteObject();
        _getHelloRemoteObject.destination = _getHelloData.destination;
        _getHelloRemoteObject.channelSet = _channelSet;
        _getHelloRemoteObject.source  = _getHelloData.source;
      
        _echoHelloRemoteObject = new RemoteObject();
        _echoHelloRemoteObject.destination = _echoHelloData.destination;
        _echoHelloRemoteObject.channelSet = _channelSet;
        _echoHelloRemoteObject.source  = _echoHelloData.source;
       }
     
       private function initOperation():void
       {  
        _getHelloOperation = new Operation();
        _getHelloOperation.name = _getHelloData.name;
      
        _echoHelloOperation = new Operation();
        _echoHelloOperation.name = _echoHelloData.name; 
       }
       private function afterInitOPeration():void
       {
        _getHelloRemoteObject.operations  = {getHello:_getHelloOperation};
        _echoHelloRemoteObject.operations = {echoHello:_echoHelloOperation};
      
        _getHelloRemoteObject.addEventListener(ResultEvent.RESULT , getHelloResult);
        _getHelloRemoteObject.addEventListener(FaultEvent.FAULT , getHelloFault);
      
        _echoHelloRemoteObject.addEventListener(ResultEvent.RESULT , echoHelloResult);
        _echoHelloRemoteObject.addEventListener(FaultEvent.FAULT , echotHelloFault);  
       }

       private function getHelloResult(e:ResultEvent):void
       {  
        getHelloOutput.text = e.result.toString();
       }
       private function getHelloFault(e:FaultEvent):void
       { 
        getHelloOutput.text = "Remote失敗";
       } 
     
       protected function echoHelloResult(e:ResultEvent):void
       {
        output.text = e.result.toString();
       }
     
       protected function echotHelloFault(e:FaultEvent):void
       {
        output.text = "Remote失敗";
       }
     
     
       private function initBtnListener():void
       {
        getHelloBtn.addEventListener(MouseEvent.MOUSE_DOWN , callGetHello);
        echoHelloBtn.addEventListener(MouseEvent.MOUSE_DOWN , callEchoHello);
       }
     
       protected function callGetHello(event:MouseEvent):void
       {
        _getHelloRemoteObject.getHello();  
       }
     
       protected function callEchoHello(event:MouseEvent):void
       {
        _echoHelloRemoteObject.echoHello(input.text);
       } 
      ]]>
     </fx:Script>
     <fx:Declarations>
      <!-- 将非可视元素(例如服务、值对象)放在此处 -->
     </fx:Declarations>
     <s:Panel width="300" height="200" title="echoHello">
      <s:layout>
       <s:VerticalLayout gap="2" paddingTop="5"/>
      </s:layout>
     
       <s:HGroup width="100%" >
        <s:Label text="回傳結果"/>
        <s:TextInput id="output"/>
       </s:HGroup> 
       <s:HGroup width="100%">
        <s:Label text="輸入數值"/>
        <s:TextInput id="input"/>
       </s:HGroup>  
       <s:Button id="echoHelloBtn" label="call echoHello"/>  
     </s:Panel>

     <s:Panel width="300" height="200" title="getHello">
      <s:layout>
       <s:VerticalLayout gap="2" paddingTop="5"/>
      </s:layout>
      <s:HGroup width="100%" >
       <s:Label text="回傳結果"/>
       <s:TextInput id="getHelloOutput"/>
      </s:HGroup>  
       <s:Button id="getHelloBtn"  label="call getHello" />
     </s:Panel>
    </s:Application>

    輸出畫面:
    






    下次將來測試一下Servlet與Mapping Value Object

    Linux上安裝tomcat6.039 (圖文版)

    連結資料:
    Apache Tomcat官網
    Tomcat6下載頁

    Step1.下載tomcat
    • 如果你的linux有圖形介面只要使用firefox到tomcat官網直接下載,下載完後檔案會再home/Downloads/底下。
    • 若沒有:
      • cd ~
      • cd Downloads
      • sudo wget http://apache.cdpa.nsysu.edu.tw/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.tar.gz
      • 輸入你的操作權限密碼
    Step2.修改檔案群組與讀取權限(若你是使用wget下載,需執行此步驟)
    • ~/Downloads$ sudo chgrp -R jeffery apache-tomcat-6.0.32.tar.gz
    • ~/Downloads$ sudo chmod -R 771 apache-tomcat-6.0.32.tar.gz
    Step3.解壓縮
    • 有圖形介面的linux直接點擊解壓縮即可。
    • 若沒有圖形介面:
      • ~/Downloads$ tar xvzf apache-tomcat-6.0.32.tar.gz
    Step4.更改資料夾名稱並將其移到/opt底下
    • ~/Downloads$ mv apache-tomcat-6.0.32 /opt/tomcat6
    Step5.啟動tomcat
    • /opt$ tomcat6/bin/./startup.sh


    執行後出現:






    Step5.使用瀏覽器測試Tomcat6有無運作
    • 瀏覽器URL輸入:http://127.0.0.1:8080/
    Tomcat預設使用Port Number為8080
    出現以下畫面即Tomcat運作成功










    Step6.修改Http使用的PortNumber與使用編碼
    • sudo gedit /opt/tomcat6/conf/server.xml
    找到:
    <Connector port= "8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />

    修改port為8400:
    <Connector port= "8400" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" URIEncoding="UTF-8"/>

    儲存變更

    server.xml是tomcat的組態設定檔

    Step7.重新啟動tomcat
    • /opt/tomcat6/bin$ ./startup.sh

    開打瀏覽器,輸入http://127.0.0.1:8400/,出現tomcat畫面即修改成功。


    Step8.替tomcat設定ubuntu桌面上快速啟動圖示
    • 滑鼠右鍵 → 新增啟動圖示
    出現操作畫面如下輸入:
    • 類型:應用程式
    • 名稱:tomcat6_startup
    • 指令:/opt/tomcat6/bin/startup.sh











    如此,方便操作tomcat的啟動,當然你可以在建立一個shutDown的操作圖示。


    Step9.新增tomcat的環境變數 (要新增此環境變數才可開發與使用servlet)

    #tomcat environment
    export CATALINA_HOME=/opt/tomcat6
    export CLASSPATH=$CLASSPATH:$CATALINA_HOME/lib:$CATALINA_HOME/lib/servlet-api.jar



    Step10.載入新設定

    • source /etc/profile


    補充說明:
    <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true"
                xmlValidation="false" xmlNamespaceAware="false">

    在Server.xml設定檔中,Host name內有一屬性unpackWARs="true",這代表會自動將war解壓縮。