Configuration

kitewormsΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

56 εμφανίσεις

Configuration.java


package com.ericsson.nrgsdk.examples.applications.guru;


import com.ericsson.nrgsdk.examples.tools.configuration.NestedProperties;


/**


* This class is responsible for parsing the configuration file and presenting


* the configuration
data using convenient data types.


*/

public class Configuration extends NestedProperties

{


/**



* The singleton instance that can be accessed by all users.



*/


public static final Configuration INSTANCE = new Configuration();



private Configuration()

{



//void


}

}


Feature.java


package com.ericsson.nrgsdk.examples.applications.guru;


import com.ericsson.hosasdk.utility.framework.FWproxy;


/**


* This class implements the logic of the application. It uses processors to


* interact with Ericsson Netw
ork Resource Gateway.


*/

public class Feature

{


private FWproxy itsFramework;


private Messenger itsMessenger;


private int itsNextQuote;



/**


* Creates a new instance of <code>Feature</code>.


*/


public Feature(FWproxy aFramework)

{



itsF
ramework = aFramework;



itsMessenger = new Messenger(this, itsFramework);


}



public void start()

{



itsMessenger.start();


}



/**


* @return a quote read from the configuration file, each time this method is called a new quote is fetched


*
/


public String getQuote()

{



String quote = Configuration.INSTANCE.getProperty("quote." + itsNextQuote);



if (quote == null)

{




quote = Configuration.INSTANCE.getProperty("quote." + (itsNextQuote = 0));



}



if (quote == null)

{




quote = "";



}



itsNextQuote++;



return quote;


}



public void stop() {



itsMessenger.stop();


}

}


GUI.java


package com.ericsson.nrgsdk.examples.applications.guru;


import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import j
avax.swing.AbstractAction;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;


/**


* This class defines a simple graphical user interface. Users can add (
tab)


* pages and buttons.


*/

public class GUI extends JPanel

{


private JButton theStartButton, theStopButton;


private Main theMain;


private JLabel theText;



/**


* Creates a frame containing placeholders for (tab) pages and buttons.


* @pa
ram aMain the parent class used for invokations from the GUI


* @param aDescription the text to display


*/


public GUI(Main aMain, String aDescription)

{



theMain = aMain;



JPanel textPanel = new JPanel();



textPanel.setLayout(new FlowLayout(Fl
owLayout.CENTER));



theText = new JLabel(aDescription);



textPanel.add(theText);



textPanel.setBorder(new EmptyBorder(8, 8, 8, 8));



JPanel buttonPanel = new JPanel();



buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));




buttonPanel.add

(

the
StartButton = new JButton(





new AbstractAction("Start")

{






public void actionPerformed(ActionEvent e)

{







theMain.start();






}





}));




buttonPanel.add ( theStopButton = new JButton(new AbstractAction("Stop")

{





public void actionPerfor
med(ActionEvent e)

{






theMain.stop();





}




}));




buttonPanel.setBorder(new EmptyBorder(0, 8, 8, 8));



setLayout(new BorderLayout());



JComponent l;



add(





l = new JLabel(Configuration.INSTANCE.getImage("banner.png")), BorderLayout.NORTH);



l.setBorder(new EmptyBorder(8, 8, 0, 8));



add(textPanel, BorderLayout.CENTER);



add(buttonPanel, BorderLayout.SOUTH);



updateState();


}



/**


* Activates or de
-
activetes the buttons in the GUI depending on the state of the application


*/


public void updateState()

{



theStartButton.setEnabled(theMain.isStopped());



theStopButton.setEnabled(theMain.isStarted());


}

}


Main.java


package com.ericsson.nrgsdk.examples.applications.guru;


import java.awt.BorderLayout;

import java.awt.event.W
indowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import javax.swing.JFrame;

import com.ericsson.hosasdk.api.HOSAMonitor;

import com.ericsson.hosasdk.utility.framework.FWproxy;

import com.ericsson.nrgsdk.examples.tools.SDKToolkit
;


/**


* This class is responsible for initializing, starting, stopping and


* terminating the application.


*/

public class Main

{


private Feature theFeature;


private GUI theGUI;


private boolean theIsStarted, theIsStopped;


private FWproxy itsFramewor
k;



/**


* Method called by the JVM to launch the application.


* @param args not used


* @throws Exception


*/


public static void main(String[] args) throws Exception {



new Main();


}



private Main() throws IOException {



HOSA
Monitor.addListener(SDKToolkit.LOGGER);



Configuration.INSTANCE.load(this);



itsFramework = new FWproxy(Configuration.INSTANCE);



theFeature = new Feature(itsFramework);



String s = "<HTML>"





+ "<font size=
\
"4
\
"><p align=center>Users can request a m
essage of wisdom by sending an
SMS to" +


" service number "+ Configuration.INSTANCE.getProperty("serviceNumber") +".</p>"





+ "<p align=center>The Guru returns an SMS with food for thought.</p></font>"





+ "</HTML>";



initGUI(s
);



theIsStopped = true;



theGUI.updateState();


}



public boolean isStarted() {



return theIsStarted;


}



public boolean isStopped() {



return theIsStopped;


}



/**


* Starts interaction with Ericsson Network Resource Gateway.


*/


publi
c void start() {



theIsStarted = theIsStopped = false;



theGUI.updateState();



try {




theFeature.start();




theIsStarted = true;



} catch (RuntimeException e) {




theIsStopped = true;




System.err.println(SDKToolkit.OBJECTWRITER.print(e));




// t
hrow e;



} finally {




theGUI.updateState();



}


}



public void stop() {



theIsStarted = theIsStopped = false;



theGUI.updateState();



try {




theFeature.stop();



} finally {




theIsStopped = true;




theGUI.updateState();



}


}



private void i
nitGUI(String aDescription) {



JFrame f = new JFrame();



f.setTitle("Guru");



f.getContentPane().setLayout(new BorderLayout());



f.getContentPane().add(theGUI = new GUI(this, aDescription),





BorderLayout.CENTER);



f.pack();



f.setLocation(100, 100
);



f.setVisible(true);



f.addWindowListener(new WindowAdapter() {





public void windowClosing(WindowEvent e) {





System.exit(0);




}



});


}

}


Messenger.java


package com.ericsson.nrgsdk.examples.applications.guru;


import com.ericsson.hosasdk.ap
i.TpAddress;

import com.ericsson.hosasdk.api.TpAddressRange;

import com.ericsson.hosasdk.api.TpHosaDeliveryTime;

import com.ericsson.hosasdk.api.TpHosaMessage;

import com.ericsson.hosasdk.api.TpHosaSendMessageReport;

import com.ericsson.hosasdk.api.TpHosaT
erminatingAddressList;

import com.ericsson.hosasdk.api.TpHosaUIMessageDeliveryType;

import com.ericsson.hosasdk.api.fw.P_UNKNOWN_SERVICE_TYPE;

import com.ericsson.hosasdk.api.hui.IpAppHosaUIManager;

import com.ericsson.hosasdk.api.hui.IpAppHosaUIManagerAda
pter;

import com.ericsson.hosasdk.api.hui.IpHosaUIManager;

import com.ericsson.hosasdk.api.ui.IpAppUI;

import com.ericsson.hosasdk.api.ui.TpUIEventCriteria;

import com.ericsson.hosasdk.api.ui.TpUIEventInfo;

import com.ericsson.hosasdk.api.ui.TpUIIdentifier
;

import com.ericsson.hosasdk.utility.framework.FWproxy;

import com.ericsson.nrgsdk.examples.tools.SDKToolkit;


public class Messenger extends IpAppHosaUIManagerAdapter implements



IpAppHosaUIManager {



private IpHosaUIManager itsService;



private FWpro
xy itsFramework;



private int itsServiceAssignmentId;



private Feature theFeature;



public Messenger(Feature aFeature, FWproxy aFramework) {



theFeature = aFeature;



itsFramework = aFramework;


}



/**


* Connect to the Ericsson Network Resourc
e Gateway and retrieve the


* SP_HOSA_USER_INTERACTION service. Then create a notification request


* for all SMS sent to the service number


*/


public void start() {


try


{


itsService = (IpHosaUIManager) itsFrame
work.obtainSCF(





IpHosaUIManager.class, "SP_HOSA_USER_INTERACTION");


}


catch (P_UNKNOWN_SERVICE_TYPE anException)


{


System.err.println("Service not found. Please refer to the Ericsson Network Resource Gateway User Gui
de for "


+ "a list of which applications that are able to run on which test tools
\
n"


+ anException);


}



TpAddressRange origin = SDKToolkit.createTpAddressRange("*");


TpAddressRange de
stination =
SDKToolkit.createTpAddressRange(Configuration.INSTANCE.getProperty("serviceNumber"));


//specifying 00 for SMS


String serviceCode = "00";


TpUIEventCriteria criteria = new TpUIEventCriteria(origin, destination,serviceCode)
;


itsServiceAssignmentId = itsService.createNotification(this, criteria);


}



/**


* Destroys the UI notification


*/


public void stop() {



try {


itsService.destroyNotification(itsServiceAssignmentId);



} finally {



// even if disposing SCS resources fails,




// we still want to dispose of our resources




this.dispose();



}


}



public IpAppUI reportNotification(TpUIIdentifier aUI, TpUIEventInfo anInfo, int i) {



TpAddress anOriginatingNumber = anInfo.Origi
natingAddress;



TpAddress aDestinationNumber = anInfo.DestinationAddress;



System.out.println("Received SMS from " + anInfo.OriginatingAddress.AddrString);



sendSMS(aDestinationNumber, anOriginatingNumber, theFeature.getQuote());



return null;


}



pri
vate void sendSMS(TpAddress anOriginatingAddress,




TpAddress aDestinationAddress, String aMessage) {



System.out.println("Sending SMS to " + aDestinationAddress.AddrString





+ ": " + aMessage);



// To send the message, the destination must be a



//
TpHosaTerminatingAddressList, instead of one address,



// so create a list with one number



TpHosaTerminatingAddressList recipients = new TpHosaTerminatingAddressList();



recipients.ToAddressList = new TpAddress[1];



recipients.ToAddressList[0] = aDest
inationAddress;



String subject = ""; // subject is not used with SMS



// Reformat the incoming message string message to a



// TpHosaMessage



TpHosaMessage message = new TpHosaMessage();



message.Text(aMessage);



// Send it as SMS



TpHosaUIMessageD
eliveryType deliveryType = TpHosaUIMessageDeliveryType.P_HUI_SMS;



String billing = "for free";



int response = 1;



boolean deliverynotification = false;



// Create a dummy delivery time (send immidiately)



TpHosaDeliveryTime deliveryTime = new TpHosa
DeliveryTime();



deliveryTime.Dummy((short) 0);



String validityTime = "2010
-
01
-
01 00:00:00.000";



// Send message



itsService.hosaSendMessageReq(this, anOriginatingAddress, recipients,





subject, message, deliveryType, billing, response,





deliver
ynotification, deliveryTime, validityTime);


}



/**



* Called remotely <br>



* Sending the message was a succes



*/


public void hosaSendMessageRes(int anAssignmentID,




TpHosaSendMessageReport[] aResponseList) {



//void


}



public void notImplement
ed() {



new UnsupportedOperationException(





"An unexpected callback method was invoked").printStackTrace();


}

}