< prev index next >

apps/samples/Ensemble8/src/samples/java/ensemble/samples/graphics3d/xylophone/XylophoneApp.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2017, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  89     private static AudioClip getNoteClip(String name) {
  90         // First look for the clips in a directory next to our jar file
  91         try {
  92             // Get a URI to this class file
  93             URI baseURI = XylophoneApp.class.getResource("XylophoneApp.class").toURI();
  94 
  95             // If we have a jar URL, get the embedded http or file URL
  96             // and trim off the internal jar path, this will leave us
  97             // with a URL to the jar file
  98             if (baseURI.getScheme().equals("jar")) {
  99                 String basePath = baseURI.getSchemeSpecificPart();
 100                 if (basePath.contains("!/")) {
 101                     basePath = basePath.substring(0, basePath.indexOf("!/"));
 102                 }
 103                 baseURI = new URI(basePath);
 104             }
 105 
 106             URL noteURL = baseURI.resolve("resources/"+name).toURL();
 107 
 108             // check if the resource exists, then try to load it
 109             if (noteURL.getProtocol().equals("http")) {
 110                 HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
 111                 urlCon.setRequestMethod("HEAD");
 112                 urlCon.connect();
 113                 if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
 114                     noteURL = null;
 115                 }
 116                 urlCon.disconnect();
 117             } else if (noteURL.getProtocol().equals("file")) {
 118                 File f = new File(noteURL.getPath());
 119                 if (!f.exists() || !f.isFile()) {
 120                     noteURL = null;
 121                 }
 122             } else {
 123                 // unsupported protocol
 124                 noteURL = null;
 125             }
 126             if (noteURL != null) {
 127                 return new AudioClip(noteURL.toExternalForm());
 128             }
 129         } catch (Exception e) {} // fail gracefully


   1 /*
   2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  89     private static AudioClip getNoteClip(String name) {
  90         // First look for the clips in a directory next to our jar file
  91         try {
  92             // Get a URI to this class file
  93             URI baseURI = XylophoneApp.class.getResource("XylophoneApp.class").toURI();
  94 
  95             // If we have a jar URL, get the embedded http or file URL
  96             // and trim off the internal jar path, this will leave us
  97             // with a URL to the jar file
  98             if (baseURI.getScheme().equals("jar")) {
  99                 String basePath = baseURI.getSchemeSpecificPart();
 100                 if (basePath.contains("!/")) {
 101                     basePath = basePath.substring(0, basePath.indexOf("!/"));
 102                 }
 103                 baseURI = new URI(basePath);
 104             }
 105 
 106             URL noteURL = baseURI.resolve("resources/"+name).toURL();
 107 
 108             // check if the resource exists, then try to load it
 109             if (noteURL.getProtocol().equals("http") || noteURL.getProtocol().equals("https")) {
 110                 HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
 111                 urlCon.setRequestMethod("HEAD");
 112                 urlCon.connect();
 113                 if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
 114                     noteURL = null;
 115                 }
 116                 urlCon.disconnect();
 117             } else if (noteURL.getProtocol().equals("file")) {
 118                 File f = new File(noteURL.getPath());
 119                 if (!f.exists() || !f.isFile()) {
 120                     noteURL = null;
 121                 }
 122             } else {
 123                 // unsupported protocol
 124                 noteURL = null;
 125             }
 126             if (noteURL != null) {
 127                 return new AudioClip(noteURL.toExternalForm());
 128             }
 129         } catch (Exception e) {} // fail gracefully


< prev index next >