Cleanup of non-graphical Java Bean

I'm currently working on a non-graphical javabean that manages serial communications with a piece of our companies electronic hardware. One of the properties of the bean is "connected", and this property governs whether or not the bean has acquired control of the serial port.
The problem I have is that when the bean is deleted it remains connected to the serial port, blocking any other connections. I want to try to clean up properly but how can I do this? As far as I can tell non-graphical beans are given no notification that they have been removed from a page.
Note that I am reluctant to make this bean graphical. I want to reserve the possibility of using it on embedded devices that may not have access to graphical libraries such as awt or swing.
Chris.

A bean that is deleted is eventually garbage collected. So, an approach would be to override the finalize() method and put your serial port disconnecton code in that method.
Here is a trivial example:
public class MyBean {
   protected void finalize() throws Throwable {
      // put your code here to disconnect serial port.
      super.finalize();
      System.out.println("disposed");
   public static void  main(String[] args){
      MyBean di = new MyBean();
      di= null;//delete the bean.
      System.gc();

Similar Messages

  • Java Bean & JDBC

    Hi ,
    How can i use a non visual java bean in an EJB .
    i want my application to work with the app server & without it . I want to
    have the business logic in java beans and reuse it in EJB.
    How can we do that..
    thanks

    Sampath wrote:
    Hi ,
    How can i use a non visual java bean in an EJB .
    i want my application to work with the app server & without it . I want to
    have the business logic in java beans and reuse it in EJB.
    How can we do that..
    thanksYou can do mostly whatever java code you want in hand-written EJBs (BMP).
    See the spec for restrictions. This group is about jdbc. Do yoiur non-visual java
    beans have anything to do with jdbc? If so, would you want their DBMS work to
    be included in the EJB transaction, or just have the EJB call them and not need
    to know what they do?
    joe

  • "Java-Bean: True" missing after being packed in jar file

    Hi All
    X86, Windows XP Professional, J2SDK 1.4.2
    I complied a section of code ", which is from << Thinking In Java>>(3rd).
    Created a manifest file "BangBean.tmp"
    <<
    Name: bangbean/BangBean.class
    Java-Bean: True
    >>
    And then packed class files into "BangBean.jar" with command
    "jar cfm BangBean.jar Bang BangBean.tmp bangbean".
    It's puzzling that "Java-Bean: Ture" was missing in the "BangBean.jar/META-INF/MENIFEST.MF" .
    <<
    Manifest-Version: 1.0
    Created-By: 1.4.2 (Sun Microsystems Inc.)
    Name: bangbean/BangBean.class
    >>
    What's more, after altering the"BangBean.tmp" into this
    <<
    Java-Bean: True
    Name: bangbean/BangBean.class
    >>
    and had a another try.
    I got a jar file with such MENIFEST.MF.
    <<
    Manifest-Version: 1.0
    Created-By: 1.4.2 (Sun Microsystems Inc.)
    Java-Bean: True
    Name: bangbean/BangBean.class
    >>
    This jar file worked well in BDK's beanbox.
    I've test it with JSDK 1.5.0, the same thing happened.
    And I didn't encounter such problem in other bean programs.
    I've searched " 'Java-Bean: True' missing " with google, but didn't get proper answers.
    Is there anybody who can give a prompt about why this happend?
    Thanks a lot for your help!
    code
    //: bangbean:BangBean.java
    // A graphical Bean.
    package bangbean;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import com.bruceeckel.swing.*;
    public class
    BangBean extends JPanel implements Serializable {
    private int xm, ym;
    private int cSize = 20; // Circle size
    private String text = "Bang!";
    private int fontSize = 48;
    private Color tColor = Color.RED;
    private ActionListener actionListener;
    public BangBean() {
    addMouseListener(new ML());
    addMouseMotionListener(new MML());
    public int getCircleSize() { return cSize; }
    public void setCircleSize(int newSize) {
    cSize = newSize;
    public String getBangText() { return text; }
    public void setBangText(String newText) {
    text = newText;
    public int getFontSize() { return fontSize; }
    public void setFontSize(int newSize) {
    fontSize = newSize;
    public Color getTextColor() { return tColor; }
    public void setTextColor(Color newColor) {
    tColor = newColor;
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.drawOval(xm - cSize/2, ym - cSize/2, cSize, cSize);
    // This is a unicast listener, which is
    // the simplest form of listener management:
    public void addActionListener(ActionListener l)
    throws TooManyListenersException {
    if(actionListener != null)
    throw new TooManyListenersException();
    actionListener = l;
    public void removeActionListener(ActionListener l) {
    actionListener = null;
    class ML extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    Graphics g = getGraphics();
    g.setColor(tColor);
    g.setFont(
    new Font("TimesRoman", Font.BOLD, fontSize));
    int width = g.getFontMetrics().stringWidth(text);
    g.drawString(text, (getSize().width - width) /2,
    getSize().height/2);
    g.dispose();
    // Call the listener's method:
    if(actionListener != null)
    actionListener.actionPerformed(
    new ActionEvent(BangBean.this,
    ActionEvent.ACTION_PERFORMED, null));
    class MML extends MouseMotionAdapter {
    public void mouseMoved(MouseEvent e) {
    xm = e.getX();
    ym = e.getY();
    repaint();
    public Dimension getPreferredSize() {
    return new Dimension(200, 200);
    } ///:~
    ************************************************

    Since you didn't use code-formatting tags ([ code ] and [ /code ] without the spaces) it's kinda hard to look at it. But I'm sure it DID execute much more than just the return statement - maybe you're not closing a file or db connection, or maybe you're seeing a cached page, so it actually isn't executing ANYTHING on the server (is your browser set to never check for newer pages so it (almost) always returns from cache, for example?)

  • Can we use an overloaded constructor of a Java Bean with Session Tracking

    Hi Friends,
    If any one can solve my query.... It would be helpful.
    Query:
    I have a Java Bean with an overloaded constructor in it. I want to use the overloaded constructor in my JSP.
    1. One way of doing that is to use it directly in the "Scriptlets" (<% %>). But then I am not sure of the way to do session tracking. I think I can use the implicit objects like "session", "request" etc. but not sure of the approach or of the implementation method.
    2. Another way is through the directive <jsp: useBean>. But I cannot call an overloaded constructor with <jsp: useBean>. The only alternative way is to use the directive <jsp: useBean> where I have to write getter and setter methods in the Java Bean and use the <jsp: setProperty> and <jsp: getProperty> standard actions. Then with this approach I cannot use the overloaded constructor.
    Can any one suggest me the best approach to solve this problem ?
    Thanks and Regards,
    Gaive.

    My first reaction is that you can refactor your overloaded constructor into an init(arguments...) method. Instead of overloaded constructor, you can call that init method. This is the ideal solution if possible.
    As to the two choices you listed:
    1. This is OK, I believe. You can use scriplet to define the bean and put it into session scope of the pageContext. I am not sure exactly what you meant by session tracking; whatever you meant, it should be doable using HttpSessionAttributeListener and/or HttpSessionBindingListener.
    2. Agreed. There is no way that <jsp:useBean> can call a constructor that has non-empty arguments.
    Please tell me how it works for you.

  • Crystal report Java Beans Connectivity

    Hi
    My name is Bach Ong. I.m currently migrating Crystal reports 10 to Crystal reports 2008 using Java Beans Connectivity. Using ResultSet as dataset. all results are return alright, EXCEPT for string type fields. The data for STRING type fields are some how suppressed. But if i executed the beans within eclipse the the string field is populated with data.
    can someone assist me with this issue.
    Thanks
    Bach Ong

    Some suggestions I would give for this are:
    1. It may be a fault with the data itself.  Specifically, if you have a string field that contains invalid or null data (Instead of an empty string), it may be causing an error to be thrown when the report engine tries to process the string fields - and it thus fails on all string fields.
    So please try this with some data where all fields contain valid non-null, non-empty data and see if it works.
    2. One of the difficulties with javabeans is that they don't give proper error messages.  However, there is a way to kick off the javabean java process in the crystal report designer which allows you to debug your javabean inside the report designer.  I have attached a sample that shows how to do that - and that should get you the real error message of what is going wrong.
    Note: Due to the way these forums work - it will only let me attach a file if it is named with the .txt extension.  You will need to extract the txt file and then rename it to .zip (annoying I know).
    Shawn

  • Java Connectivity - No classes appearing in the Java Beans Classes dropdown

    Hi,
    VERY NEW user to Crystal Reports.  I am trying to create a report using a java class as a datasource.  I have updated the setting in the CRConfig.xml file, and can get the Java Beans Connectivity screen to appear, but is shows none of my classes in the dropdown.  I found a knowlege base entry that indicated this problem was caused by the JavaBeansClassPath value in the CRConfig.xml file being invalid.  But I've verified the location and it is pathed directly to the classes.
    Not sure what I am missing, but any help is greatly appreciated.
    Thanks!

    Ted,
    Thanks for the reply.  I checked out each of your suggestions, but haven't had any luck thus far.  Here's what I found:
    1.  We created a simple method in an existing public class to return a ResultSet.  The method looks like this:
    public ResultSet GetAllMediaTypesRS()
          String strFuncName = "ScsXDMDatabase::GetAllMediaTypesRS : ";
          ScsDebug coutDebug = new ScsDebug();
          String strSqlStmt;
          String strMediaType;
          ScsStringList sslReturnList = new ScsStringList();
          // Create the SQL to get all the Displayable Media Types.
          strSqlStmt =
             "SELECT DISTINCT mt.Media_Type_Name "
                + "FROM dbo.Media_Type mt "
                + "WHERE mt.Media_Type_Rqst_Display_Ind = 1 "
                + "ORDER BY mt.Media_Type_Name";
          coutDebug.println(strFuncName + "SQL Statement:  " + strSqlStmt);
          // Run the Query
          Statement stmt = null;
          ResultSet SQLResult = null;
          try
             stmt = GetConnection().createStatement();
             SQLResult = stmt.executeQuery(strSqlStmt);
          } //End of Try
          catch (SQLException sqle)
             if (sqle.getErrorCode() != 0)
                coutDebug.println(
                   "Error in ExecuteQuery: "
                      + "Error code - "
                      + sqle.getErrorCode());
                coutDebug.println("Message - " + sqle.getMessage());
          return SQLResult;
    The signature, and the method look fine to us, but maybe we are missing something obvious.
    2. We do use JDK 1.5 to compile the classes, but my JavaDir setting in the CRConfig.xml file points to jre1.6.0_02.  I assume this is OK.
    3. We do see java.exe start up when opening the JavaBeans connection.
    4. No firewall issues as we are inside our corporate network.
    Any other thoughts or ideas?  I did see a knowledge base entry that indicated the cause of this particular problem is that the JavaBeansClassPath in CRConfig.xml is incorrect or invalid.  I've validated the path takes me right to the classes, so I believe that is OK.  Is there some way that we can see the actual path that is searched when the JavaBeans connection is opened?
    Thanks for your help.
    --john davies

  • Java Bean Thread Safety

    I've been using JavaBeans for years now, and have read at least 30 different texts and online resources about using Java beans in JSPs. But in every single one of them, they are ambiguous and non-committal about how to properly use them to avoid Thread-Safety problems in a multithreaded (web server) environment. Are there any true Java gurus out there who know how to do so in JSPs?
    Specifically, when you use the <jsp:useBean> tag, that tag automatically binds an instance of your bean to the PageContext object. Since the PageContext object is shared by many threads, wouldn't this automatically make all of the Java Bean's properties vulnerable for thread problems? Since the pageContext is shared between threads, wouldnt one have to declare every one of the bean's setters and getters as "synchronized", to prevent one thread overwriting another's values?
    I ask because in many texts, they make a vague suggestion "be sure to write your beans thread-safe"--but provide no concrete answer as to how (the level at which to declare 'synchronized'). But in all their code examples, I have never once, in all these years, seen the word "synchronized".
    How is this possible? Wouldn't the bean, as bound to the thread-shared pageContext object, be completely exposed to thread corruption, with multiple threads simultaneously calling its methods?
    Can someone supply some code snippets showing the thread-safe way to use JavaBeans (i.e., where to synchronize)

    PageContext is shared by many threads?
    Not at one time, I'm pretty certain of that.
    From the API: A PageContext instance is obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().
    The to me suggests the contract that a PageContext can only be in use by on one JSP page at a time. As far as I am concerned, pageContext can be treated like a "local variable" to a jsp page.
    The contents of the pageContext object are maybe a different story, but we'll get to that.
    The things to worry about with thread safety are the same as they are for servlets.
    1 - Class attributes are not threadsafe. ie variables declared within <%! %> signs
    2 - Session attributes are potentially not threadsafe if the user makes two quick requests in a row (handling two requests for the same session)
    3 - Application attributes are never threadsafe as any currently running request can access them. Most Application level attributes I treat as read only (kinda like Singleton access)
    I don't see the need to go overboard declaring everything "synchronized". In fact I think it would be hugely detrimental.

  • Java Bean to check for Well-formed  XML DOC?

    Hi All,
    I am new to the Java XML API's. Does anyone have an example of
    a Java Bean that i can pass an XML String and check if the
    XML document is well formed?
    Thanks
    Joe

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));try/catch this ugly line, if no parse exception and a non -null doc exists then your XML is well-formed.
    of course if your XML contains namespaces, you would have to setup correctly the document builder.

  • Writing java bean class?

    Hi All,
    In my scenario,I am getting all pagelevel details from the database.
    I mean Suppose in my web application 10 pages are there.for 10 pages, all details (like page title,button names,page header etc) based on user.per user it will vary .Some body have insert,update buttons ,some body not.
    I was created java bean with static getters and setters for all pages to get the data and store.
    I don't want to connect to the database to get the page details per every page.so,
    its working for me.what happen if more than one user access the application.The details will effect to all when new user enter into the system?
    any suggestions.....................................????
    if I write non - static getters and setters ,I need to instantiate everypage.
    its takes more memory ,In my web application I have above 75 pages.
    solution???

    1)So,I need to pass the Class object to the
    session.setAttribute()
    right?
    Well to be precise - you create an instance of the class (object) for each registered and logged in user and then yes - store the object as a session attribute
    >
    2)Suppose I change my bean to instance bean,bean have
    above 20 instance variables .I think performance
    point of view,Its not good.
    Because,if users are more than 20 what happend?
    You should probably think about 200 :). 20 users is not an issue wwhen you have sufficient hardware.
    One option would be to store the user permission/preferences in the db and associate it with the user id. Infact for large applications that hold lots of user related data per user, that would be my preferred approach.
    ram.

  • LightDM: How to stop non-graphical applications on session logout?

    Hi all,
    I'm currently moving from XDM to LightDM and have a hard time figuring out how to clean up after myself when my session ends.
    Here's the problem:
    I start two non-graphical applications in ~/.xprofile
    offlineimap
    ssh-agent
    I want to cleanly kill these when my graphical session ends
    What would be the best way to achieve this?
    What I tried until now:
    Add ~/.xlogout which does the cleanup
    Create /etc/lightdm/Xsession-mine which calls /etc/lightdm/Xsession and then ~/.xlogout
    Modify /etc/lightdm/lightdm.conf to use /etc/lightdm/Xsession-mine as session wrapper
    Result: ~/.xlogout doesn't seem to be called, probably due to exec in Xsession
    History:
    With XDM I would start my window manager in the foreground (without exec) in ~/.xinitrc and when it returns call my cleanup code.
    Thanks in advance for all help and suggestions!
    Timo

    Hi graysky,
    graysky wrote:Haven't used lightdm in a long time... does it have a PostLogout function similar to lxdm?
    https://wiki.archlinux.org/index.php/Lxdm#Configuration
    it does have something alike:
    # session-cleanup-script = Script to run when quitting a user session (runs as root)
    However, as the documentation states, this script is run as root, so I can't clean up on a per-user basis (or can I?).
    Kind regards,
    Timo

  • Java Bean In Forms6i

    Hi,
    Can a java bean be called from a form executing in Client server environment.
    If yes how can it b achieved?
    Thanks & Regards,
    Abhijeet

    Not if you need to use any graphical component of it. For this you'll need to run on the Web.
    If you just want to invoke the logic of the bean try using the Java importer.

  • Non Graphical Game?

    Hi, I was wondering if it is at all possible to make a non-graphical game with the java scripting program. I've seen so many posts about graphical games.. also, if there is a language that fits more with non-graphic interfaces (text based), please say so.

    Java is great for muds and the like...if that is the type you are looking to do. Combined with beanshell (scripting) you can do some very robust/dynamic things.

  • Scope of Java Bean in portal pages

    Hello All,
    My objective is to share information between different portal pages using java bean.
    I have portal page (say page1) having a portlet which is a jsp (say jsp1). In JSP1, I have following use bean tag( please see, the scope is Application)
    <jsp:usebean id='userSession',scope='application',class='...'>
    (As per my understanding of 'application' scope, this java bean will be visible to all the JSPs in the current browser session.)
    I set some of the properties of the bean after this.
    Now, from my 2nd portal page (say page 2) having exactly one portlet which is again a jsp (say jsp2), I try to access the same javaBean using the use bean tag.
    <jsp:usebean id='userSession',scope='application',class='...'>
    When I try to access the properties of the bean, none of them are available.
    It looks like the original instance of the bean is lost and new instance is created.
    Don't Java beans behave in the same way inside and outside the Portal environment ?
    If I can't use java beans, could you please suggest any other way where I can share some data between Portal pages.
    Thanks very much in Anticipation.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Naresh Kumar ([email protected]):
    Hello All,
    My objective is to share information between different portal pages using java bean.
    When I try to access the properties of the bean, none of them are available.
    It looks like the original instance of the bean is lost and new instance is created.
    Don't Java beans behave in the same way inside and outside the Portal environment ?
    If I can't use java beans, could you please suggest any other way where I can share some data between Portal pages.
    Thanks very much in Anticipation.<HR></BLOCKQUOTE>
    I have the same problem.
    Why Portal behaviour is not transparent with Javabeans ?
    G.Grimoldi
    null

  • Convert xml to java bean

    hi all
    i want to know how to convert a XML file to a java bean and this bean should be integrated with obejectweb,a open source middleware.
    1. how to create beans in NetBeans5.5
    2. how to run the bean
    3. how to connect the xml file to a bean program

    I think you need to start over and read a few tutorials. You cannot "run" a bean (unless it has a main() method but that would be very non-standard). You should not worry about which IDE you are using (Eclipse, NetBeans, etc.) And you do not "connect" the XML file to the bean, which again is not a program.
    The technologies you want to use will be JAXB (java XML binding) or Apache's XMLBeans. Both will require you to write a XML schema, which those API's can then use to create JavaBeans for you. There are tutorials for JAXB here at Sun and at Apache for XMLBeans.
    - Saish

  • Custom tags v/s java beans

    Hi all,
    Is there any thumb rule when to use Custom Tag and when to use Java Beans.
    I feel we can get the solution using either of them.
    Am I right?
    Cheers

    Hi all,
    Is there any thumb rule when to use Custom Tag and
    when to use Java Beans.
    I feel we can get the solution using either of them.
    Am I right?
    Usually you can indeed use either. Custom tags will create cleaner JSPs with less (if any) Java code, which makes it a lot easier for non-Java people to maintain them.
    Custom beans are often quicker to create, and therefore better suited as interim solutions or for pages that won't be around for long (maybe that one-time action?).
    Of course custom tags will often also make use of beans :)

Maybe you are looking for

  • Intermittent problems with iphone 3g "charging not supported"

    I notice quite a few complaints about intermittent 'charging not supported' messages. I have been getting the same message on my iphone 3g. I have ELIMINATED the cord and plug as the problem in this case by the following method: I found that by LOWER

  • THE AV DIGITAL ADAPTER MODEL IS MC953ZM/A

    THE APPLE AV DIGITAL ADAPTER MODEL IS MC953ZM/A. DOES THIS ADAPTER WORK WITH THE IPOD CLASSIC 2012? SO I CAN CONNECT TO A TV WITH A HDMI CONNECTION?

  • Java - what does it do?

    What is the advantage of using Java, and any advice for the software upgrade? Thanks for any clear information. PT

  • Convert video into iPhone video?

    I have a lot of videos that I'd like to send over to my iPhone. Problem is that most (if not, all) do not open in iTunes so I they will not get transfered over to the iPhone. Is there any free mac software out there that can convert almost any video

  • Assign Bank account to the Customer

    Hi, How to assign bank account to the customer?? in AR or CM???? pls help me n step by step fashion... thanks a lot n advance..