OSDN Git Service

Merge branch 'temp1'
[luz/luz.git] / src / com / lavans / luz2 / lremote / connector / impl / SingleConnector.java
diff --git a/src/com/lavans/luz2/lremote/connector/impl/SingleConnector.java b/src/com/lavans/luz2/lremote/connector/impl/SingleConnector.java
new file mode 100644 (file)
index 0000000..82b5764
--- /dev/null
@@ -0,0 +1,113 @@
+package com.lavans.luz2.lremote.connector.impl;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.BufferedOutputStream;\r
+import java.io.IOException;\r
+import java.io.ObjectInputStream;\r
+import java.io.ObjectOutputStream;\r
+import java.net.URLConnection;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.omg.CORBA.portable.ApplicationException;\r
+\r
+import com.lavans.luz2.lremote.connector.Connector;\r
+import com.lavans.luz2.lremote.node.RemoteNode;\r
+\r
+public class SingleConnector implements Connector{\r
+       /** logger */\r
+       private static Log logger = LogFactory.getLog(RemoteNode.class.getName());\r
+\r
+       /** main connection */\r
+       private RemoteNode remoteNode = null;\r
+       private URLConnection con = null;\r
+\r
+\r
+       /**\r
+        * Constructor.\r
+        */\r
+       public SingleConnector(RemoteNode value){\r
+               this.remoteNode = value;\r
+       }\r
+\r
+       public boolean init(){\r
+               try {\r
+                       con = remoteNode.getUrl().openConnection();\r
+                       con.setConnectTimeout(1000);\r
+                       con.setRequestProperty("Connection", "Keep-Alive");\r
+                       con.setDoOutput(true);\r
+                       con.connect();\r
+                       return true;\r
+               } catch (IOException e) {\r
+                       logger.warn("create connection error["+remoteNode.getUrl()+ "("+ e.getMessage() +")]");\r
+                       logger.debug(null, e);\r
+                       return false;\r
+               }\r
+       }\r
+\r
+       /**\r
+        * AP�֖₢���킹�������ĉ����I�u�W�F�N�g�����炤�B\r
+        * AP���ŃL���b�`������O��ApplicationException�Ƃ��ċA���Ă���̂�\r
+        * �����I�u�W�F�N�g��ApplicationException�������ꍇ�͂��̂܂�throw����B\r
+        * AP�Ƃ̐ڑ��G���[�̏ꍇ�͂P�x�����Đڑ������݂�B����ȊO�̃G���[�͂�����\r
+        * ApplicationException�ɂ�����Action�֓`����B\r
+        *\r
+        * �u�]�͕s���Œ�����t�s�\81v�Ȃǂ̃G���[��Exception�ł͂Ȃ�\r
+        * �߂�I�u�W�F�N�g�̒��̃p�����[�^�ɓ���Ă���B\r
+        *\r
+        * @param className\r
+        * @param methodName\r
+        * @param paramTypes\r
+        * @param args\r
+        * @return\r
+        * @throws ApplicationException\r
+        */\r
+       public Object execute(String className, String methodName,\r
+                       Class<?>[] paramTypes, Object[] args) throws Exception{\r
+               String[] shortNames = className.split("\\.");\r
+               String shortName = shortNames[shortNames.length - 1];\r
+               logger.debug("remote execute "+ shortName +"#"+methodName +"()");\r
+               Object result = null;\r
+               ObjectOutputStream os = null;\r
+               ObjectInputStream is = null;\r
+               try {\r
+                       os = new ObjectOutputStream(\r
+                                       new BufferedOutputStream(con.getOutputStream()));\r
+                       // �N���X��\r
+                       os.writeObject(className);\r
+                       // ���\�b�h��\r
+                       os.writeObject(methodName);\r
+                       // ��̌^\r
+                       os.writeObject(paramTypes);\r
+                       // ��\r
+                       os.writeObject(args);\r
+                       // �o�b�t�@���t���b�V�����đ��M\r
+                       os.flush();\r
+\r
+                       // ��M����\r
+                       is = new ObjectInputStream(\r
+                                       new BufferedInputStream(con.getInputStream()));\r
+                       result = is.readObject();\r
+\r
+                       // �󂯎�������̂���O��������(AP�ŗ�O���N�����ꍇ)\r
+                       if(result instanceof Exception){\r
+                               logger.info("result is exception", (Exception)result);\r
+                               // �G���[���b�Z�[�W������Ă���̂ł��̂܂܃X���[����B\r
+                               throw (Exception)result;\r
+                       }\r
+               } finally {\r
+                       try { os.close(); } catch (Exception e) { logger.warn(null, e); }\r
+                       try { if(is!=null)is.close(); } catch (Exception e) { logger.warn(null, e); }\r
+               }\r
+\r
+               return result;\r
+       }\r
+\r
+       /**\r
+        * @return con\r
+        */\r
+       public RemoteNode getremoteNode() {\r
+               return remoteNode;\r
+       }\r
+\r
+}\r