1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.example.imagine.step3.server;
21
22 import java.awt.Color;
23 import java.awt.Font;
24 import java.awt.Graphics;
25 import java.awt.image.BufferedImage;
26
27 import javax.management.MBeanServer;
28 import javax.management.ObjectName;
29
30 import org.apache.mina.core.service.IoHandlerAdapter;
31 import org.apache.mina.core.session.IoSession;
32 import org.apache.mina.example.imagine.step1.ImageRequest;
33 import org.apache.mina.example.imagine.step1.ImageResponse;
34 import org.apache.mina.integration.jmx.IoSessionMBean;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39 * server-side {@link org.apache.mina.core.service.IoHandler}
40 *
41 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42 */
43
44 public class ImageServerIoHandler extends IoHandlerAdapter {
45
46 private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
47
48 public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
49
50 private static Logger LOGGER = LoggerFactory.getLogger(ImageServerIoHandler.class);
51
52 private MBeanServer mBeanServer;
53
54 /**
55 * Creates a new instance of ImageServerIoHandler. For this step, we pass in a reference
56 * to the MBeanServer. This instance will be used to register new IoSession objects
57 * so that the JMX subsystem can report statistics on the sessions.
58 *
59 * @param mBeanServer
60 * The JMX MBeanServer that will register the sessions
61 */
62 public ImageServerIoHandler( MBeanServer mBeanServer ) {
63 this.mBeanServer = mBeanServer;
64 }
65
66 /**
67 * This method is called first when a new connection to the server is made. In here we will set
68 * up the JMX session MBean.
69 *
70 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionCreated(org.apache.mina.core.session.IoSession)
71 */
72 public void sessionCreated( IoSession session ) throws Exception
73 {
74 // create a session MBean in order to load into the MBeanServer and allow
75 // this session to be managed by the JMX subsystem.
76 IoSessionMBean sessionMBean = new IoSessionMBean( session );
77
78 // create a JMX ObjectName. This has to be in a specific format.
79 ObjectName sessionName = new ObjectName( session.getClass().getPackage().getName() +
80 ":type=session,name=" + session.getClass().getSimpleName() + "-" + session.getId());
81
82 // register the bean on the MBeanServer. Without this line, no JMX will happen for
83 // this session
84 mBeanServer.registerMBean( sessionMBean, sessionName );
85 }
86
87 /**
88 * Called when the session is opened, which will come after the session created.
89 *
90 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
91 */
92 public void sessionOpened(IoSession session) throws Exception {
93
94 // set the index to zero. This is used to determine how the build the
95 // string that is sent to the client.
96 session.setAttribute(INDEX_KEY, 0);
97 }
98
99 /**
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 }