{"id":24457,"date":"2021-04-10T23:10:04","date_gmt":"2021-04-11T03:10:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/implement-your-own-permission-pretty-simple-java-code-root\/"},"modified":"2021-04-10T23:10:04","modified_gmt":"2021-04-11T03:10:04","slug":"implement-your-own-permission-pretty-simple-java-code-root","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=24457","title":{"rendered":"Implement your Own Permission: Pretty Simple Java Code #Root"},"content":{"rendered":"<p>Implementing Your Own Permission<br \/>\npackage com.gamedev.games;<\/p>\n<p>import java.io.*;<br \/>\nimport java.security.*;<br \/>\nimport java.util.Hashtable;<br \/>\nimport com.scoredev.scores.*;<\/p>\n<p>public class ExampleGame<br \/>\n{<br \/>\n    public static void main(String args[])<br \/>\n\tthrows Exception<br \/>\n    {<br \/>\n\tHighScore hs = new HighScore(&#8220;ExampleGame&#8221;);<\/p>\n<p>\tif (args.length == 0)<br \/>\n\t    usage();<\/p>\n<p>\tif (args[0].equals(&#8220;set&#8221;)) {<br \/>\n\t    hs.setHighScore(Integer.parseInt(args[1]));<br \/>\n\t} else if (args[0].equals(&#8220;get&#8221;)) {<br \/>\n\t    System.out.println(&#8220;score = &#8220;+ hs.getHighScore());<br \/>\n\t} else {<br \/>\n\t    usage();<br \/>\n\t}<br \/>\n    }<\/p>\n<p>    public static void usage()<br \/>\n    {<br \/>\n\tSystem.out.println(&#8220;ExampleGame get&#8221;);<br \/>\n\tSystem.out.println(&#8220;ExampleGame set &#8220;);<br \/>\n\tSystem.exit(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>&#8212;<\/p>\n<p>\/*<br \/>\n * Copyright (c) 1995, 2008, Oracle and\/or its affiliates. All rights reserved.<br \/>\n *<br \/>\n * Redistribution and use in source and binary forms, with or without<br \/>\n * modification, are permitted provided that the following conditions<br \/>\n * are met:<br \/>\n *<br \/>\n *   &#8211; Redistributions of source code must retain the above copyright<br \/>\n *     notice, this list of conditions and the following disclaimer.<br \/>\n *<br \/>\n *   &#8211; Redistributions in binary form must reproduce the above copyright<br \/>\n *     notice, this list of conditions and the following disclaimer in the<br \/>\n *     documentation and\/or other materials provided with the distribution.<br \/>\n *<br \/>\n *   &#8211; Neither the name of Oracle or the names of its<br \/>\n *     contributors may be used to endorse or promote products derived<br \/>\n *     from this software without specific prior written permission.<br \/>\n *<br \/>\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &#8220;AS<br \/>\n * IS&#8221; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,<br \/>\n * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR<br \/>\n * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR<br \/>\n * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,<br \/>\n * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,<br \/>\n * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR<br \/>\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF<br \/>\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br \/>\n * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS<br \/>\n * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br \/>\n *\/<\/p>\n<p>package com.scoredev.scores;<\/p>\n<p>import java.io.*;<br \/>\nimport java.security.*;<br \/>\nimport java.util.Hashtable;<\/p>\n<p>public class HighScore<br \/>\n{<br \/>\n    private String gameName;<br \/>\n    private File highScoreFile;<\/p>\n<p>    public HighScore(String gameName)<br \/>\n    {<br \/>\n    this.gameName = gameName;<\/p>\n<p>    AccessController.doPrivileged(new PrivilegedAction() {<br \/>\n        public Object run() {<br \/>\n        String path =<br \/>\n            System.getProperty(&#8220;user.home&#8221;) +<br \/>\n            File.separator +<br \/>\n            &#8220;.highscore&#8221;;<\/p>\n<p>        highScoreFile = new File(path);<br \/>\n        return null;<br \/>\n        }<br \/>\n    });<br \/>\n    }<\/p>\n<p>    public void setHighScore(final int score)<br \/>\n    throws IOException<br \/>\n    {<br \/>\n    \/\/check permission first<br \/>\n    SecurityManager sm = System.getSecurityManager();<br \/>\n    if (sm != null) {<br \/>\n        sm.checkPermission(new HighScorePermission(gameName));<br \/>\n    }<\/p>\n<p>    \/\/ need a doPrivileged block to manipulate the file<br \/>\n    try {<br \/>\n        AccessController.doPrivileged(new PrivilegedExceptionAction() {<br \/>\n        public Object run() throws IOException {<br \/>\n            Hashtable scores = null;<br \/>\n            \/\/ try to open the existing file. Should have a locking<br \/>\n            \/\/ protocol (could use File.createNewFile).<br \/>\n            try {<br \/>\n            FileInputStream fis =<br \/>\n                new FileInputStream(highScoreFile);<br \/>\n            ObjectInputStream ois = new ObjectInputStream(fis);<br \/>\n            scores = (Hashtable) ois.readObject();<br \/>\n            } catch (Exception e) {<br \/>\n            \/\/ ignore, try and create new file<br \/>\n            }<\/p>\n<p>            \/\/ if scores is null, create a new hashtable<br \/>\n            if (scores == null)<br \/>\n            scores = new Hashtable(13);<\/p>\n<p>            \/\/ update the score and save out the new high score<br \/>\n            scores.put(gameName, new Integer(score));<br \/>\n            FileOutputStream fos = new FileOutputStream(highScoreFile);<br \/>\n            ObjectOutputStream oos = new ObjectOutputStream(fos);<br \/>\n            oos.writeObject(scores);<br \/>\n            oos.close();<br \/>\n            return null;<br \/>\n        }<br \/>\n        });<br \/>\n    } catch (PrivilegedActionException pae) {<br \/>\n        throw (IOException) pae.getException();<br \/>\n    }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * get the high score. return -1 if it hasn&#8217;t been set.<br \/>\n     *<br \/>\n     *\/<br \/>\n    public int getHighScore()<br \/>\n    throws IOException, ClassNotFoundException<br \/>\n    {<br \/>\n    \/\/check permission first<br \/>\n    SecurityManager sm = System.getSecurityManager();<br \/>\n    if (sm != null) {<br \/>\n        sm.checkPermission(new HighScorePermission(gameName));<br \/>\n    }<\/p>\n<p>    Integer score = null;<\/p>\n<p>    \/\/ need a doPrivileged block to manipulate the file<br \/>\n    try {<br \/>\n         score = (Integer) AccessController.doPrivileged(<br \/>\n                                    new PrivilegedExceptionAction() {<br \/>\n        public Object run()<br \/>\n            throws IOException, ClassNotFoundException<br \/>\n        {<br \/>\n            Hashtable scores = null;<br \/>\n            \/\/ try to open the existing file. Should have a locking<br \/>\n            \/\/ protocol (could use File.createNewFile).<br \/>\n            FileInputStream fis =<br \/>\n            new FileInputStream(highScoreFile);<br \/>\n            ObjectInputStream ois = new ObjectInputStream(fis);<br \/>\n            scores = (Hashtable) ois.readObject();<\/p>\n<p>            \/\/ get the high score out<br \/>\n            return scores.get(gameName);<br \/>\n        }<br \/>\n        });<br \/>\n    } catch (PrivilegedActionException pae) {<br \/>\n        Exception e = pae.getException();<br \/>\n        if (e instanceof IOException)<br \/>\n        throw (IOException) e;<br \/>\n        else<br \/>\n        throw (ClassNotFoundException) e;<br \/>\n    }<br \/>\n    if (score == null)<br \/>\n        return -1;<br \/>\n    else<br \/>\n        return score.intValue();<br \/>\n    }<\/p>\n<p>    public static void main(String args[])<br \/>\n    throws Exception<br \/>\n    {<br \/>\n    HighScore hs = new HighScore(args[1]);<br \/>\n    if (args[0].equals(&#8220;set&#8221;)) {<br \/>\n        hs.setHighScore(Integer.parseInt(args[2]));<br \/>\n    } else {<br \/>\n        System.out.println(&#8220;score = &#8220;+ hs.getHighScore());<br \/>\n    }<br \/>\n    }<br \/>\n}<\/p>\n<p> From: http:\/\/sitestree.com\/?p=10884<br \/> Categories:Root<br \/>Tags:<br \/> Post Data:2017-07-20 22:37:31<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing Your Own Permission package com.gamedev.games; import java.io.*; import java.security.*; import java.util.Hashtable; import com.scoredev.scores.*; public class ExampleGame { public static void main(String args[]) throws Exception { HighScore hs = new HighScore(&#8220;ExampleGame&#8221;); if (args.length == 0) usage(); if (args[0].equals(&#8220;set&#8221;)) { hs.setHighScore(Integer.parseInt(args[1])); } else if (args[0].equals(&#8220;get&#8221;)) { System.out.println(&#8220;score = &#8220;+ hs.getHighScore()); } else { usage(); } } &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=24457\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-24457","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10104,"url":"http:\/\/bangla.sitestree.com\/?p=10104","url_meta":{"origin":24457,"position":0},"title":"Example Java Programs","author":"","date":"August 3, 2015","format":false,"excerpt":"HelloWorld.java public class HelloWorld { \u00a0\u00a0\u00a0 \/\/ method main(): ALWAYS the APPLICATION entry point \u00a0\u00a0\u00a0 public static void main (String[] args) { \u00a0\u00a0 \u00a0System.out.println (\"Hello World!\"); \u00a0\u00a0\u00a0 } } \/\/ Print Today's Date import java.util.*; public class HelloDate { \u00a0\u00a0\u00a0 public static void main (String[] args) { \u00a0\u00a0 \u00a0System.out.println (\"Hello,\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26864,"url":"http:\/\/bangla.sitestree.com\/?p=26864","url_meta":{"origin":24457,"position":1},"title":"Example Java Programs #Programming Code Examples #Java\/J2EE\/J2ME #J2SE","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"Very Simple Java Example Programs HelloWorld.java public class HelloWorld { \/\/ method main(): ALWAYS the APPLICATION entry point public static void main (String[] args) { System.out.println (\"Hello World!\"); } } \/\/ Print Today's Date import java.util.*; public class HelloDate { public static void main (String[] args) { System.out.println (\"Hello, it's:\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10194,"url":"http:\/\/bangla.sitestree.com\/?p=10194","url_meta":{"origin":24457,"position":2},"title":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.","author":"","date":"August 25, 2015","format":false,"excerpt":"UrlRetriever2.java\u00a0 Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class \u00a0*\u00a0 instead of connecting explicitly to the HTTP server. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10185,"url":"http:\/\/bangla.sitestree.com\/?p=10185","url_meta":{"origin":24457,"position":3},"title":"TokTest.java Illustrates parsing a string with a StringTokenizer.","author":"","date":"August 22, 2015","format":false,"excerpt":"TokTest.java\u00a0 Illustrates parsing a string with a StringTokenizer. import java.util.StringTokenizer; \/** Prints the tokens resulting from treating the first \u00a0*\u00a0 command-line argument as the string to be tokenized \u00a0*\u00a0 and the second as the delimiter set. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10120,"url":"http:\/\/bangla.sitestree.com\/?p=10120","url_meta":{"origin":24457,"position":4},"title":"EmployeeTest2.java: A test case for the database utilities. Prints results formatted as an HTML table.","author":"","date":"August 5, 2015","format":false,"excerpt":"package cwp; import java.sql.*; \/** Connect to Oracle or Sybase and print \"employees\" table \u00a0*\u00a0 as an HTML table. \u00a0* \u00a0 \u00a0*\/ public class EmployeeTest2 { \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 if (args.length < 5) { \u00a0\u00a0\u00a0\u00a0\u00a0 printUsage(); \u00a0\u00a0\u00a0\u00a0\u00a0 return; \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 String vendorName = args[4];\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10130,"url":"http:\/\/bangla.sitestree.com\/?p=10130","url_meta":{"origin":24457,"position":5},"title":"PreparedStatements.java An example to test the timing differences resulting from repeated raw queries vs. repeated calls","author":"","date":"August 7, 2015","format":false,"excerpt":"package cwp; import java.sql.*; \/** An example to test the timing differences resulting \u00a0*\u00a0 from repeated raw queries vs. repeated calls to \u00a0*\u00a0 prepared statements. These results will vary dramatically \u00a0*\u00a0 among database servers and drivers. With my setup \u00a0*\u00a0 and drivers, Oracle prepared statements took only half \u00a0*\u00a0 the\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/24457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24457"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/24457\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24457"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}