001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.management;
020
021import java.io.*;
022
023import javax.management.remote.generic.ObjectWrapping;
024
025import org.jppf.comm.socket.BootstrapObjectSerializer;
026
027/**
028 * This implementation uses the configured JPPF serialization scheme.
029 * @author Laurent Cohen
030 * @exclude
031 */
032public class CustomWrapping2 implements ObjectWrapping {
033  /**
034   * 
035   */
036  private static final BootstrapObjectSerializer SERIALIZER = new BootstrapObjectSerializer();
037
038  @Override
039  public Object unwrap(final Object wrapped, final ClassLoader cl) throws IOException, ClassNotFoundException {
040    try {
041      Object o = ((WrappedObject) wrapped).object;
042      return o;
043    } catch (Exception e) {
044      throw new IOException(e);
045    }
046  }
047
048  @Override
049  public Object wrap(final Object obj) throws IOException {
050    try {
051      return new WrappedObject(obj);
052    } catch (Exception e) {
053      throw new IOException(e);
054    }
055  }
056
057  /**
058   * 
059   */
060  public static class WrappedObject implements Serializable {
061    /**
062     * The object to wrap.
063     */
064    public Object object;
065
066    /**
067     * 
068     * @param object the object to wrap.
069     */
070    public WrappedObject(final Object object) {
071      this.object = object;
072    }
073
074    /**
075     * Save the state of the notification to a stream (i.e. serialize it).
076     * @param out the output stream to which to write the job. 
077     * @throws IOException if any I/O error occurs.
078     */
079    private void writeObject(final ObjectOutputStream out) throws IOException {
080      try {
081        SERIALIZER.serialize(object, out, false);
082      } catch (IOException e) {
083        throw e;
084      } catch (Exception e) {
085        throw new IOException(e);
086      }
087    }
088
089    /**
090     * Reconstitute the notification instance from an object stream (i.e. deserialize it).
091     * @param in the input stream from which to read the job. 
092     * @throws IOException if any I/O error occurs.
093     * @throws ClassNotFoundException if the class of an object in the object graph can not be found.
094     */
095    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
096      try {
097        object = SERIALIZER.deserialize(in, false);
098      } catch (IOException|ClassNotFoundException e) {
099        throw e;
100      } catch (Exception e) {
101        throw new IOException(e);
102      }
103    }
104  }
105}