View Javadoc

1   // Copyright (C) 2005  Michael Wever ( mick@wever.org )
2   // 
3   // This program is free software; you can redistribute it and/or
4   // modify it under the terms of the GNU General Public License
5   // as published by the Free Software Foundation; either version 2
6   // of the License, or (at your option) any later version.
7   // 
8   // This program is distributed in the hope that it will be useful,
9   // but WITHOUT ANY WARRANTY; without even the implied warranty of
10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  // GNU General Public License for more details.
12  // 
13  // You should have received a copy of the GNU General Public License
14  // along with this program; if not, write to the Free Software
15  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  /*
17   * SelectionServlet.java
18   *
19   * Created on 23. juni 2005, 19:13
20   */
21  
22  package org.wever.jradiodj.servlets;
23  
24  import java.io.*;
25  import java.util.concurrent.locks.ReentrantLock;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  
29  import javax.servlet.*;
30  import javax.servlet.http.*;
31  import javax.xml.parsers.ParserConfigurationException;
32  import javax.xml.transform.TransformerException;
33  import org.wever.jradiodj.beans.SongCollection;
34  import org.wever.jradiodj.services.ServiceFactory;
35  import org.wever.jradiodj.services.collection.CollectionManager;
36  import org.wever.jradiodj.services.playlist.PlaylistGenerator;
37  
38  /*** Displays music available to play.
39   * Collection location is defined through System property: org.wever.jradiodj.music.location
40   *
41   * @author mick
42   * @version
43   */
44  public final class SelectionServlet extends HttpServlet {
45  
46      private static SongCollection playing = null;
47      private final static ReentrantLock scanningLock = new ReentrantLock();
48      // business domain usage
49      private CollectionManager cManager = null;
50      private PlaylistGenerator plGenerator = null;
51      
52      private final static Logger LOG = Logger.getLogger(SelectionServlet.class.getName());
53      
54      
55      static{
56          LOG.setLevel(Level.FINEST);
57          System.setProperty("org.xml.sax.driver","org.apache.xerces.parsers.SAXParser");
58          System.setProperty("javax.xml.parsers.DocumentBuilderFactory","org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
59      }
60      
61      /*** Initializes the servlet.
62       */
63      public void init(ServletConfig config) throws ServletException {
64          super.init(config);
65          
66          // temporary code
67          System.setProperty("org.wever.jradiodj.musicLocations","/var/music");
68          System.setProperty("org.wever.jradiodj.playlist.file","/home/icecast/playlist.txt");
69          
70          // business domain usage
71          cManager = ServiceFactory.getCollectionManager();
72          plGenerator = ServiceFactory.getPlaylistGenerator();
73          // start scanning of the music collection
74          final Thread scan = new Thread() {
75              public void run() {
76                  scanForMusic();
77              }
78          };
79          scan.start();
80          try {
81              Thread.currentThread().sleep(100);
82          } catch(InterruptedException ie) {
83              LOG.warning(ie.getLocalizedMessage());
84          }
85      }
86      
87      /*** Destroys the servlet.
88       */
89      public void destroy() {
90          
91      }
92      
93      /*** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
94       * @param request servlet request
95       * @param response servlet response
96       */
97      protected void processRequest(
98              final HttpServletRequest request, 
99              final HttpServletResponse response) throws ServletException, IOException {
100          
101         final boolean html = request.getRequestURI().contains("html");
102         
103         response.setContentType( html ? "text/html" : "text/xml");
104         response.setCharacterEncoding("UTF-8");
105         
106         // Forwarding
107         if( html ) {
108             getServletContext().getRequestDispatcher("/xhtml.jsp").forward(request, response);
109             
110         } else {
111 
112             final Writer writer = response.getWriter();
113             try {
114                 if( scanningLock.isLocked() ) {
115                     writer.write(cManager.getProcessingState().toXMLString());
116                 } else {
117                     writer.write(cManager.getCollection().toXMLString());
118                 }
119             } catch(ParserConfigurationException pce ){
120                 LOG.warning(pce.getLocalizedMessage());
121             } catch(TransformerException te) {
122                 LOG.warning(te.getLocalizedMessage());
123                   
124             } finally {
125                 writer.close();
126             }
127         }
128         
129         
130     }
131     
132     /*** Handles the HTTP <code>GET</code> method.
133      * @param request servlet request
134      * @param response servlet response
135      */
136     protected void doGet(
137             final HttpServletRequest request, 
138             final HttpServletResponse response) throws ServletException, IOException {
139         
140         processRequest(request, response);
141     }
142     
143     /*** Handles the HTTP <code>POST</code> method.
144      * @param request servlet request
145      * @param response servlet response
146      */
147     protected void doPost(
148             final HttpServletRequest request, 
149             final HttpServletResponse response) throws ServletException, IOException {
150         
151         final String[] artists = request.getParameterValues("artists");
152         if( artists != null && artists.length >0 ) {
153             playing = new SongCollection();
154             for(String artist : artists) {
155                 
156                 for( File file : cManager.getCollection().getFilesForArtist(artist) ) {
157                     playing.addArtist(artist, file);
158                 }
159             }
160             plGenerator.createPlaylist(playing);
161         }
162         processRequest(request, response);
163     }
164     
165     /*** Returns a short description of the servlet.
166      */
167     public String getServletInfo() {
168         return "Displays music available to play.\n"
169                 +"Collection location is defined through System property: org.wever.jradiodj.music.location";
170     }
171     
172     private synchronized void scanForMusic() {
173         scanningLock.lock();
174         try {
175             
176             // get list of songs, this starts the scanning if it already hasn't started
177             cManager.getCollection();
178         } finally {
179             scanningLock.unlock();
180         }
181     }
182     
183 }