001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  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,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.mina.example.imagine.step3.server;
021
022import java.awt.Color;
023import java.awt.Font;
024import java.awt.Graphics;
025import java.awt.image.BufferedImage;
026
027import javax.management.MBeanServer;
028import javax.management.ObjectName;
029
030import org.apache.mina.core.service.IoHandlerAdapter;
031import org.apache.mina.core.session.IoSession;
032import org.apache.mina.example.imagine.step1.ImageRequest;
033import org.apache.mina.example.imagine.step1.ImageResponse;
034import org.apache.mina.integration.jmx.IoSessionMBean;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * server-side {@link org.apache.mina.core.service.IoHandler}
040 *
041 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
042 */
043
044public class ImageServerIoHandler extends IoHandlerAdapter {
045
046    private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
047
048    public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
049
050    private static Logger LOGGER = LoggerFactory.getLogger(ImageServerIoHandler.class);
051
052    private MBeanServer mBeanServer;
053    
054    /**
055     * Creates a new instance of ImageServerIoHandler.  For this step, we pass in a reference
056     * to the MBeanServer.  This instance will be used to register new IoSession objects
057     * so that the JMX subsystem can report statistics on the sessions.
058     *
059     * @param mBeanServer
060     *  The JMX MBeanServer that will register the sessions
061     */
062    public ImageServerIoHandler( MBeanServer mBeanServer ) {
063        this.mBeanServer = mBeanServer;
064    }
065    
066    /**
067     * This method is called first when a new connection to the server is made.  In here we will set
068     * up the JMX session MBean.
069     * 
070     * @see org.apache.mina.core.service.IoHandlerAdapter#sessionCreated(org.apache.mina.core.session.IoSession)
071     */
072    public void sessionCreated( IoSession session ) throws Exception
073    {
074        // create a session MBean in order to load into the MBeanServer and allow
075        // this session to be managed by the JMX subsystem.
076        IoSessionMBean sessionMBean = new IoSessionMBean( session );
077        
078        // create a JMX ObjectName.  This has to be in a specific format.  
079        ObjectName sessionName = new ObjectName( session.getClass().getPackage().getName() + 
080            ":type=session,name=" + session.getClass().getSimpleName() + "-" + session.getId());
081        
082        // register the bean on the MBeanServer.  Without this line, no JMX will happen for
083        // this session
084        mBeanServer.registerMBean( sessionMBean, sessionName );
085    }
086
087    /**
088     * Called when the session is opened, which will come after the session created.
089     * 
090     * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
091     */
092    public void sessionOpened(IoSession session) throws Exception {
093        
094        // set the index to zero.  This is used to determine how the build the
095        // string that is sent to the client.
096        session.setAttribute(INDEX_KEY, 0);
097    }
098
099    /**
100     * This method will be called whenever an exception occurs.  For this handler,
101     * the logger will generate a warning message.
102     * 
103     * @see org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
104     */
105    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
106        LOGGER.warn(cause.getMessage(), cause);
107    }
108
109    /**
110     * Handle incoming messages.
111     * 
112     * @see org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
113     */
114    public void messageReceived(IoSession session, Object message) throws Exception {
115        ImageRequest request = (ImageRequest) message;
116        String text1 = generateString(session, request.getNumberOfCharacters());
117        String text2 = generateString(session, request.getNumberOfCharacters());
118        BufferedImage image1 = createImage(request, text1);
119        BufferedImage image2 = createImage(request, text2);
120        ImageResponse response = new ImageResponse(image1, image2);
121        session.write(response);
122    }
123
124    /**
125     * Create an image using the specified request and the text.  
126     *
127     * @param request
128     *  Determines the height and width of the image
129     * @param text
130     *  The text that is placed in the image
131     * @return
132     *  a BufferedImage representing the text.
133     */
134    private BufferedImage createImage(ImageRequest request, String text) {
135        BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
136        Graphics graphics = image.createGraphics();
137        graphics.setColor(Color.YELLOW);
138        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
139        Font serif = new Font("serif", Font.PLAIN, 30);
140        graphics.setFont(serif);
141        graphics.setColor(Color.BLUE);
142        graphics.drawString(text, 10, 50);
143        return image;
144    }
145
146    /**
147     * Generate a string based on the 'characters' field in this class.  The
148     * characters that make up the string are based on the session 
149     * attribute "INDEX_KEY"
150     *
151     * @param session
152     *  The {@link IoSession} object that will provide the INDEX_KEY attribute
153     * @param length
154     *  The length that the String will be
155     * @return
156     *  The generated String
157     */
158    private String generateString(IoSession session, int length) {
159        Integer index = (Integer) session.getAttribute(INDEX_KEY);
160        StringBuilder buffer = new StringBuilder(length);
161        while (buffer.length() < length) {
162            buffer.append(characters.charAt(index));
163            index++;
164            if (index >= characters.length()) {
165                index = 0;
166            }
167        }
168        session.setAttribute(INDEX_KEY, index);
169        return buffer.toString();
170    }
171
172}