Permalink
Browse files

Added configuration

  • Loading branch information...
1 parent 7f9eec7 commit bbe1c6f2e03d56b94ccb17e19ffa515140462dc2 @foch committed Apr 10, 2012
View
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
- <classpathentry kind="src" output="test-classes" path="test"/>
<classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
<classpathentry kind="con" path="com.google.gwt.eclipse.core.GWT_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
@@ -9,8 +9,11 @@
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class RabbitMessenger implements EntryPoint {
+
private final ApplicationController controller = new ApplicationController();
+ private static String rabbitName;
+
/**
* This is the entry point method.
*/
@@ -23,10 +26,21 @@ public void onModuleLoad() {
mainView.setPresenter(messagePresenter);
- // get the rabbit status
- messagePresenter.getStatus();
+ // get the rabbit name first
+ messagePresenter.fetchRabbitName();
+ // get the rabbit status
+ messagePresenter.fetchRabbitStatus();
+
RootPanel.get("mainPanel").add(mainView);
-
}
+
+ public static String getRabbitName() {
+ return rabbitName;
+ }
+
+ public static void setRabbitName(String rabbitName) {
+ RabbitMessenger.rabbitName = rabbitName;
+ }
+
}
@@ -1,6 +1,7 @@
package com.rabbitmessenger.client.presenter;
import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.rabbitmessenger.client.RabbitMessenger;
import com.rabbitmessenger.client.service.MessageServiceAsync;
import com.rabbitmessenger.client.view.MessageView;
@@ -17,7 +18,7 @@ public MessagePresenter(MessageServiceAsync messageService,
this.messageView = messageView;
}
- private static final String SERVER_ERROR = "Il y a eu un erreur. Essaye une autre fois !";
+ private static final String SERVER_ERROR = "Il y a eu une erreur. Essaye une autre fois !";
private static final String MESSAGE_SENT = "Ton message a été envoyé à Heisenberg !";
private static final String MP3_SENT = "Ton mp3 a été envoyé à Heisenberg !";
@@ -38,7 +39,7 @@ public void onFailure(Throwable caught) {
}
@Override
- public void getStatus() {
+ public void fetchRabbitStatus() {
messageService.getStatus(new AsyncCallback<Boolean>() {
@Override
@@ -69,4 +70,20 @@ public void onSuccess(Boolean result) {
});
}
+ @Override
+ public void fetchRabbitName() {
+ messageService.getRabbitName(new AsyncCallback<String>() {
+
+ @Override
+ public void onFailure(Throwable caught) {
+ messageView.showMessage(SERVER_ERROR);
+ }
+
+ @Override
+ public void onSuccess(String result) {
+ RabbitMessenger.setRabbitName(result);
+ }
+ });
+ }
+
}
@@ -9,6 +9,8 @@
@RemoteServiceRelativePath("message")
public interface MessageService extends RemoteService {
+ String getRabbitName();
+
boolean sendMessage(String name, String message)
throws IllegalArgumentException;
@@ -11,4 +11,6 @@
void getStatus(AsyncCallback<Boolean> callback);
void playMP3(String mp3, AsyncCallback<Boolean> callback);
+
+ void getRabbitName(AsyncCallback<String> callback);
}
@@ -4,9 +4,10 @@
public interface MessageView {
interface Presenter {
+ void fetchRabbitName();
void sendMessage(String name, String message);
void playMP3(String mp3);
- void getStatus();
+ void fetchRabbitStatus();
}
void showMessage(String result);
@@ -11,6 +11,7 @@
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
+import com.rabbitmessenger.client.RabbitMessenger;
import com.rabbitmessenger.client.view.MessageView;
public class MessageWidget extends Composite implements MessageView {
@@ -21,6 +22,7 @@
private static final MessageUiBinder uiBinder = GWT
.create(MessageUiBinder.class);
+
@UiField
Label statusLabel;
@@ -68,9 +70,9 @@ public void setPresenter(Presenter presenter) {
@Override
public void setStatus(boolean status) {
if (status) {
- statusLabel.setText("Heisenberg est allumé !!! Envoie lui un message.");
+ statusLabel.setText(RabbitMessenger.getRabbitName() + " est allumé !!! Envoie lui un message.");
} else {
- statusLabel.setText("Heisenberg est éteint :( Repasse plus tard.");
+ statusLabel.setText(RabbitMessenger.getRabbitName() + " est éteint :( Repasse plus tard.");
}
messageButton.setEnabled(status);
@@ -0,0 +1,53 @@
+package com.rabbitmessenger.server;
+
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author julien
+ */
+public final class ConfigurationManager {
+
+ private static final Logger log = Logger
+ .getLogger(ConfigurationManager.class.getName());
+
+ private static ConfigurationManager instance;
+
+ private Properties properties;
+
+ public static ConfigurationManager getInstance() {
+ if (instance == null) {
+ instance = new ConfigurationManager("config.properties");
+ }
+ return instance;
+ }
+
+ private ConfigurationManager(String configFile) {
+ properties = new Properties();
+ InputStream fileInputStream;
+ try {
+ fileInputStream = this.getClass().getClassLoader()
+ .getResourceAsStream(configFile);
+
+ properties.load(fileInputStream);
+ fileInputStream.close();
+ } catch (Exception e) {
+ log.severe("Error reading configuration file: " + e.getMessage());
+ }
+ }
+
+ public String getRabbitName() {
+ return properties.getProperty("name");
+ }
+
+ public String getToken() {
+ return properties.getProperty("token");
+ }
+
+ public String getSerialNumber() {
+ return properties.getProperty("sn");
+ }
+
+}
@@ -15,6 +15,14 @@
private static final Logger log = Logger.getLogger(MessageServiceImpl.class
.getName());
+ public static final ConfigurationManager config = ConfigurationManager
+ .getInstance();
+
+ @Override
+ public String getRabbitName() {
+ return config.getRabbitName();
+ }
+
@Override
public boolean sendMessage(String name, String message)
throws IllegalArgumentException {
@@ -95,4 +103,5 @@ private String escapeHtml(String html) {
return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
.replaceAll(">", "&gt;").replaceAll(" ", "%20").toLowerCase();
}
+
}
@@ -9,26 +9,30 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
+
public class RabbitCommunication {
public static AtomicInteger spaceCounter = new AtomicInteger(0);
- public static final String TOKEN = "token=f9cb4224-b6a7-4b41-b600-510df70167a5&sn=0013D38114F2";
+ public static final ConfigurationManager config = ConfigurationManager
+ .getInstance();
private static final Logger log = Logger
.getLogger(RabbitCommunication.class.getName());
public static boolean sendMessage(String message) {
- String url = "http://api.nabaztag.com/vl/api.jsp?" + TOKEN + "&tts="
- + message;
+ String url = "http://api.nabaztag.com/vl/api.jsp?token="
+ + config.getToken() + "&sn=" + config.getSerialNumber()
+ + "&tts=" + message;
return sendURL(url);
}
public static boolean playMP3(String message) {
- String url = "http://api.nabaztag.com/vl/FR/api_stream.jsp?" + TOKEN
+ String url = "http://api.nabaztag.com/vl/FR/api_stream.jsp?token="
+ + config.getToken() + "&sn=" + config.getSerialNumber()
+ "&urlList=" + message;
return sendURL(url);
@@ -37,12 +41,12 @@ public static boolean playMP3(String message) {
private static boolean sendURL(String inputUrl) {
// add some spaces at the end of the URL to avoid caching by Google.
-// int addSpaces = spaceCounter.incrementAndGet() % 32;
-// StringBuilder sb = new StringBuilder(inputUrl);
-// for (int i = 0; i < addSpaces; i++) {
-// sb.append("%20");
-// }
-// inputUrl = sb.toString();
+ // int addSpaces = spaceCounter.incrementAndGet() % 32;
+ // StringBuilder sb = new StringBuilder(inputUrl);
+ // for (int i = 0; i < addSpaces; i++) {
+ // sb.append("%20");
+ // }
+ // inputUrl = sb.toString();
inputUrl = inputUrl.replaceAll(" ", "%20");
log.fine("Calling URL: " + inputUrl);
@@ -51,29 +55,31 @@ private static boolean sendURL(String inputUrl) {
try {
URL url = new URL(inputUrl);
-
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestProperty("Cache-Control", "max-age=0");
- connection.setDoOutput(true);
-
- if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- // OK
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- String line;
-
- while ((line = reader.readLine()) != null) {
- log.fine("Received: " + line);
- result = "<string>ok</string>".equals(line);
- break;
- }
- reader.close();
- } else {
- // Server returned HTTP error code.
- log.severe("Received HTTP error code: " + connection.getResponseCode());
- return false;
- }
-
+
+ HttpURLConnection connection = (HttpURLConnection) url
+ .openConnection();
+ connection.setRequestProperty("Cache-Control", "max-age=0");
+ connection.setDoOutput(true);
+
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+ // OK
+ BufferedReader reader = new BufferedReader(
+ new InputStreamReader(connection.getInputStream()));
+ String line;
+
+ while ((line = reader.readLine()) != null) {
+ log.fine("Received: " + line);
+ result = "<string>ok</string>".equals(line);
+ break;
+ }
+ reader.close();
+ } else {
+ // Server returned HTTP error code.
+ log.severe("Received HTTP error code: "
+ + connection.getResponseCode());
+ return false;
+ }
+
} catch (MalformedURLException e) {
log.severe("MalformedURLException: " + e.getLocalizedMessage());
return false;
View
@@ -0,0 +1,3 @@
+name=Heisenberg
+token=123456789
+sn=123456789

0 comments on commit bbe1c6f

Please sign in to comment.