{"id":854,"date":"2010-04-08T12:02:16","date_gmt":"2010-04-08T11:02:16","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=854"},"modified":"2010-04-08T12:04:12","modified_gmt":"2010-04-08T11:04:12","slug":"rmi-client-and-run-the-library","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-client-and-run-the-library\/","title":{"rendered":"RMI &#8211; Client and run the Library"},"content":{"rendered":"<p>This is the last part in the build up to the RMI (Remote Method Invocation) this is the clients part, the previous part was the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-the-library-factory\/\">RMI library<\/a> \/ server.  Where the server will listen for a clients request.<\/p>\n<p>So to start with we need to be able to call the remote server, so we need to be able to have permission from the RMI java security package.<\/p>\n<pre lang=\"java\">\r\nSystem.setSecurityManager(new RMISecurityManager());\r\n<\/pre>\n<p>next we need to build up the RMI server request URL (where the server is and also the remote class that it is listening on and as taken from <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-the-library-factory\/\">here<\/a> we are listening on the &#8220;RemoteBook&#8221; class) (we are listening on the localhost as the server)<\/p>\n<pre lang=\"java\">\r\nString remoteClass = \"RemoteBook\";\r\nString rmiURL = \"rmi:\/\/localhost\/\" + remoteClass;\r\n<\/pre>\n<p>since all the client needs to know is what <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-interface\/\">interface<\/a> has been implemented within the server (the borrowable interface) then we just need to create a new object based on that that then links to the servers listening class rmiURL (as taken from above)<\/p>\n<pre lang=\"java\">\r\nBorrowable remoteBook = (Borrowable)Naming.lookup(rmiURL);\r\n<\/pre>\n<p>and then just call the functions that will have been implemented on the server (the server has used the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-implementation-of-the-library-book\/\">Book<\/a>.java which implements the Borrowable interface) because now they are linked.<\/p>\n<p>The java <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/rmi\/Naming.html\">Naming<\/a> object is a RMI object that allows for the server to bind&#8217;s its class object to a URL and the client to link its interface object to the servers class object basically.<\/p>\n<p>here is the client code in full<\/p>\n<pre lang=\"java\">\r\nimport java.util.*;\r\nimport java.rmi.*;\r\n\r\npublic class RMIClient {\r\n\tString remoteClass = \"RemoteBook\";\r\n\tString libraryCardNumber = \"genuxCard\";\r\n\t\r\n\tpublic RMIClient()\r\n\t{\r\n\t\tif (System.getSecurityManager()== null)\r\n\t\t\tSystem.setSecurityManager(new RMISecurityManager());\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic boolean useLibrary()\r\n\t{\r\n\t\tboolean result = true;\r\n\t\ttry {\r\n\t\t\tString rmiURL = \"rmi:\/\/localhost\/\" + remoteClass;\r\n\t\t\tBorrowable remoteBook = (Borrowable)Naming.lookup(rmiURL);\r\n\t\t\tSystem.out.println(\"The book is checked out \"+remoteBook.isCheckedOut());\r\n\t\t\t\r\n\t\t\tboolean outResult = remoteBook.checkOut(libraryCardNumber,new Date());\r\n\t\t\t\r\n\t\t\tif (outResult==true)\r\n\t\t\t\tSystem.out.println(\"Book checked out successfully\");\r\n\t\t\telse\r\n\t\t\t\tresult = false;\r\n\t\t\t\r\n\t\t\tif (remoteBook.isCheckedOut())\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"libraray Card number that has checked out the book is :\"+remoteBook.checkedOutCardNumber());\r\n\t\t\t}\r\n\r\n\t\t\tboolean inResult = remoteBook.checkIn(libraryCardNumber, new Date());\r\n\t\t\t\r\n\t\t\tif (inResult==true)\r\n\t\t\t\tSystem.out.println(\"Book checked in\");\r\n\t\t\telse\r\n\t\t\t\tresult = false;\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"RMI Error: \"+e.getMessage());\r\n\t\t\tresult = false;\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tRMIClient RMIC = new RMIClient();\r\n\t\tboolean result = RMIC.useLibrary();\r\n\t\tif (result == false)\r\n\t\t\tSystem.out.println(\"Error using library\");\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>and if you save that as RMIClient.java, compile up the java files to create the class files<\/p>\n<p>This is the policy file, if you save this as RMIpolicyfile.policy, it basically allows all permissions within the java security object, not the best security for servers but this will server as a local host test. (alter the where\/your\/class\/files\/are to where you are building up the java files into there class files, within <a href=\"http:\/\/www.eclipse.org\/\">eclipse<\/a> it has a \/src directory and a \/bin so you would link to the \/bin directory) <\/p>\n<pre lang=\"java\">\r\ngrant codeBase \"file:\/where\/your\/class\/files\/are\" {\r\n    permission java.security.AllPermission;\r\n};\r\n<\/pre>\n<p>and now that you have the RMI server (RMIFactory) and the RMI client (RMIClient) all we need to do is to register the java RMI to allow connections on the linux command line I do <\/p>\n<pre lang=\"bash\">\r\nrmiregistry &\r\n<\/pre>\n<p>the &#8220;&#038;&#8221; allows it to run in the back ground, then to run the server<\/p>\n<pre lang=\"bash\">\r\njava -Djava.security.policy=RMIpolicyfile.policy RMIFactory &\r\nconsole output : Remote book object has been started\r\n<\/pre>\n<p>which this uses the security policy file from above and also runs again in the background (&#8220;&#038;&#8221;) and now to just run the client<\/p>\n<pre lang=\"bash\">\r\njava -Djava.security.policy=RMIpolicyfile.policy RMIClient\r\n<\/pre>\n<p>once again since the client is requesting the RMI security, you need to tell it to use the RMI policy file from above, and the output would be<\/p>\n<pre lang=\"bash\">\r\nThe book is checked out false\r\nBook checked out successfully\r\nlibraray Card number that has checked out the book is :genuxCard Date taken out on Thu Apr 08 11:49:36 BST 2010\r\nBook checked in\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the last part in the build up to the RMI (Remote Method Invocation) this is the clients part, the previous part was the RMI library \/ server. Where the server will listen for a clients request. So to start with we need to be able to call the remote server, so we need &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-client-and-run-the-library\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RMI &#8211; Client and run the Library<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[165,174,172],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-java","tag-client","tag-policy-file","tag-rmi"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/854\/revisions\/856"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}