001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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      final Object o = ((WrappedObject) wrapped).object;
042      return o;
043    } catch (final 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 (final Exception e) {
053      throw new IOException(e);
054    }
055  }
056
057  /**
058   * 
059   */
060  public static class WrappedObject implements Serializable {
061    /**
062     * Explicit serialVersionUID.
063     */
064    private static final long serialVersionUID = 1L;
065    /**
066     * The object to wrap.
067     */
068    public Object object;
069
070    /**
071     * 
072     * @param object the object to wrap.
073     */
074    public WrappedObject(final Object object) {
075      this.object = object;
076    }
077
078    /**
079     * Save the state of the notification to a stream (i.e. serialize it).
080     * @param out the output stream to which to write the job. 
081     * @throws IOException if any I/O error occurs.
082     */
083    private void writeObject(final ObjectOutputStream out) throws IOException {
084      try {
085        SERIALIZER.serialize(object, out, false);
086      } catch (final IOException e) {
087        throw e;
088      } catch (final Exception e) {
089        throw new IOException(e);
090      }
091    }
092
093    /**
094     * Reconstitute the notification instance from an object stream (i.e. deserialize it).
095     * @param in the input stream from which to read the job. 
096     * @throws IOException if any I/O error occurs.
097     * @throws ClassNotFoundException if the class of an object in the object graph can not be found.
098     */
099    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
100      try {
101        object = SERIALIZER.deserialize(in, false);
102      } catch (final IOException|ClassNotFoundException e) {
103        throw e;
104      } catch (final Exception e) {
105        throw new IOException(e);
106      }
107    }
108  }
109}