AudioSecurityManager.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* AudioSecurityManager.java -- Manages Security requests for Sound classes.
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.sound;
  32. import javax.sound.sampled.AudioPermission;
  33. /**
  34. * This class handles security requests for classes in the Sound API.
  35. *
  36. * A class that needs to check against a particular permission type may use this
  37. * class to query the <code>SecurityManager</code>.
  38. *
  39. * For example, to check for a read permission, a class can simply pass the
  40. * <code>Permission.READ</code> constant to
  41. * {@link #checkPermissions(gnu.javax.sound.AudioSecurityManager.Permission))},
  42. * like the following code demonstrates:
  43. *
  44. * <pre>
  45. * AudioSecurityManager.checkPermissions(Permission.PLAY);
  46. * </pre>
  47. *
  48. * If there is need to query for all the defined permissions type, the constant
  49. * <code>Permission.ALL</code> can be used. In alternative, the
  50. * {@link #checkPermissions()} is presented as a shorthand.
  51. *
  52. * @author Mario Torre <neugens@limasoftware.net>
  53. */
  54. public class AudioSecurityManager
  55. {
  56. /**
  57. * Defines a common set of permission allowed by the specification.
  58. */
  59. public static enum Permission
  60. {
  61. PLAY, RECORD, ALL
  62. }
  63. /**
  64. * Shorthand to <code>checkPermissions(Permission.ALL)</code>.
  65. */
  66. public static final void checkPermissions()
  67. {
  68. checkPermissions(Permission.ALL);
  69. }
  70. /**
  71. * Query the <code>SecurityManager</code> agains the given
  72. * <code>Permission</code>.
  73. *
  74. * @param permission
  75. */
  76. public static final void checkPermissions(Permission permission)
  77. {
  78. SecurityManager sm = System.getSecurityManager();
  79. if (sm != null)
  80. {
  81. String perm = null;
  82. switch (permission)
  83. {
  84. case PLAY:
  85. perm = "play";
  86. break;
  87. case RECORD:
  88. perm = "record";
  89. break;
  90. case ALL: default:
  91. perm = "*";
  92. break;
  93. }
  94. sm.checkPermission(new AudioPermission(perm));
  95. }
  96. }
  97. }