Home | History | Annotate | Download | only in module
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 package org.opensolaris.os.vp.panels.example.time2.server.module;
     28 
     29 import java.io.IOException;
     30 import java.text.*;
     31 import java.util.Date;
     32 import javax.management.*;
     33 import org.opensolaris.os.vp.panels.example.time2.common.*;
     34 import org.opensolaris.os.vp.util.misc.ProcessUtil;
     35 import org.opensolaris.os.vp.util.misc.exception.CommandFailedException;
     36 
     37 public class Time implements TimeMXBean, MBeanRegistration {
     38     //
     39     // MBeanRegistration methods
     40     //
     41 
     42     @Override
     43     public void postDeregister() {
     44     }
     45 
     46     @Override
     47     public void postRegister(Boolean registrationDone) {
     48     }
     49 
     50     @Override
     51     public void preDeregister() {
     52     }
     53 
     54     @Override
     55     public ObjectName preRegister(MBeanServer server, ObjectName name) {
     56 	if (name == null) {
     57 	    name = TimeUtil.OBJECT_NAME;
     58 	}
     59 	return name;
     60     }
     61 
     62     //
     63     // TimeMXBean methods
     64     //
     65 
     66     @Override
     67     public long getTime() {
     68 	return System.currentTimeMillis();
     69     }
     70 
     71     @Override
     72     public void setTime(long millis) throws IOException {
     73 	Date date = new Date(millis);
     74 	DateFormat format = new SimpleDateFormat("MMddHHmmyyyy.ss");
     75 	String dateString = format.format(date);
     76 
     77 	try {
     78 	    ProcessUtil.exec(0, "/usr/bin/date", dateString);
     79 
     80 	} catch (CommandFailedException e) {
     81 	    throw new IOException(e);
     82 
     83 	// Not likely
     84 	} catch (InterruptedException e) {
     85 	    throw new IOException("command interrupted");
     86 	}
     87     }
     88 }
     89