Вы находитесь на странице: 1из 27

MindTree C2

Tr aining As signment s
in
Cor e J ava

MindTree Consulting Private Limited, #42, 27th Cross, Banashankari II Stage


Bangalore - 560 070 www.mindtree.com
MindTree C2 | Assignments

Table of Contents
Instructions.....................................................................................................................................................3
Problem 1 Calendar Printer..........................................................................................................................5
Problem 2 Syntax Highlight.........................................................................................................................6
Problem 3 Queue Monitor............................................................................................................................8
Problem 4 Priority Queue...........................................................................................................................10
Problem 5 Thread Pool................................................................................................................................11
Problem 6 Find Pattern in Files..................................................................................................................13
Problem 7 Multi-value Properties.............................................................................................................14
Problem 8 Simple Encryption / Decryption.............................................................................................15
Problem 9 Socket Server..............................................................................................................................16
Problem 10 Simple File Transfer................................................................................................................17
Problem 11 Bean to Map.............................................................................................................................19
Problem 12 Classpath expansion...............................................................................................................21
Problem 13 Class file disassembler...........................................................................................................22
Problem 14 Object Cache............................................................................................................................23
Problem 15 TCP Snoop...............................................................................................................................24
Problem 16 JDBC Connection Pool...........................................................................................................26

Training Assignments Page 2 of 27


MindTree C2 | Assignments

Instructions
Please read the following instructions carefully before you proceed.

1. This document contains a list of 16 assignments that you need to solve as part of your
training objectives. All assignments are “hands-on” -- there are no theory questions. All
assignments require implementation in the form of source code, documentation and
compiled binaries to be submitted for evaluation.

2. All problems are related to the Core Java domain. The target development platform is Eclipse
IDE of version 3.x.x and later. The target Java platform is Java 2 Standard Edition version
1.4.x by Sun Microsystems.

3. Your code must be organized into appropriate package structures. All packages must start
with mindtree.training. Every assignment has an associated problem id. You should use
this id as the name of the package for the corresponding solution after pre-pending the same
with mindtree.training. For example, the package name for the solution to problem #1
should be mindtree.training.calprint. You can create additional nested sub-packages
as necessary.

4. You must adhere to the Java coding standards as proposed by Sun Microsystems and
available from their web site. All code must be properly indented for better readability.

5. All code must be properly documented using the documentation features provided by the
Java platform itself (javadoc). Your submission must also contain the generated
documentation.

6. Each assignment must be a separate project under Eclipse. The project root folder must have
the same name as that of the problem id. For example, the project root folder for problem #1
can be any one of D:\training\calprint; C:\mywork\calprint; etc. Avoid creating
project roots under C:\My Documents; C:\Temp or any of the system folders used by
windows. Since an Eclipse installation will be a shared resource, do not create your projects
under the Eclipse installation folder.

7. Within an eclipse project, you must have separate folders for Java sources, compiled classes,
generated documentation and third party libraries. Sources should go under the folder
sources, classes should go under classes, generated documentation under docs and
third party libraries under lib.

8. Your submission for each assignment will be a ZIP file containing a snapshot of the entire
project directory under Eclipse. You should use the export feature in Eclipse to perform this
operation.

9. Multiple assignments must not share the same project space.

Training Assignments Page 3 of 27


MindTree C2 | Assignments

10. Avoid writing all code in one file. Each source file must contain only one non-inner Java class.

11. Do not hard-code literal or numeric values inside your program. Either define the same in an
interface or read from a properties file on disk.

12. Do not intermix your testing code with the core implementation. Create a separate sub
package for the same.

13. Many of the assignments have output that are generic and reusable. The core implementation
for the same must not be accompanied by a main() method. Such methods should appear in
classes that are under the sub-packages containing testing stubs.

14. Cleanup your code before submission. Check for missing documentation, stray method,
variable and import declarations (Eclipse will help you do this).

15. Many of the assignments build on top of one another. Do not copy code from one project to
another (unless the same has been suitably modified to meet the needs of the target
assignment). Instead you should make the Eclipse project for the target assignment refer to
the Eclipse project for the base assignment for classpath resolution.

16. Every problem is associated with a set of instructions. You must strictly adhere to the
instructions provided.

Training Assignments Page 4 of 27


MindTree C2 | Assignments

Problem 1 Calendar Printer


Problem ID: calprint

Description:

The Calendar printer is a console-based application that prints the calendar for a given month on
screen. The month and the year values are provided as parameters to the main() method. The
output of the application should look like this:

June 2005

Sun Mon Tue Wed Thu Fri Sat


---------------------------
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
---------------------------

It should be possible to provide the input month and year in a variety of formats. For example,
June 2005; Jun 2005; 06-2005; 06-05 should all be treated as valid input information and
interpreted accordingly.

Instructions:

1. The main class for the application must be called CalendarPrinter.

2. The output calendar format as shown above must be strictly adhered to. (Note the spacing
between columns, especially for the single digit values)

3. You need to make use of the calendar classes from the java.util package. You also need to
use the text parsing classes from the java.text package for interpreting the input month
and year.

Training Assignments Page 5 of 27


MindTree C2 | Assignments

Problem 2 Syntax Highlight


Problem ID: stxcol

Description:

Many popular editors provide a syntax highlight feature that colorizes a given text buffer based
on programming language semantics. Here’s an example to illustrate the point:

public class ClassName extends SuperClass


{ private static final int CONSTANT= 0;
/* This comment may span multiple lines. */
private static int staticField = 0;
// This comment may span only this line
private String field = "zero";

public int foo(int parameter)


{ abstractMethod();
int local= 42 * hashCode();
staticMethod();
return bar(local) + 24;
}
}

Write a utility module that understands the semantics of the Java programming language for
syntax highlighting. The input to the module is a text buffer containing the Java source code. The
output of the module is a text buffer containing HTML code fragment that displays the contents
of the input data with syntax coloring.

Some of the aspects of the Java syntax that needs to be highlighted are:

• Keywords like import, public, class, interface, etc.

• Strings enclosed within double quotes and characters enclosed within single quotes.

• Operators like braces, parenthesis, assignment, etc.

• Single line and multi-line comments.

The syntax highlight module should be configurable to define color values for different items. To
test the module, write a separate application that invokes this module. The application should
read a Java source file (.java extension) from disk and save the output to another file with the
same name but with a .html extension. For example, if the source file is called test.java the
generated output file should be called test.html.

Training Assignments Page 6 of 27


MindTree C2 | Assignments

Instructions:

1. The entry class for the syntax highlight module must be called SyntaxHighlight. The API
signature for this class is as follows:

public class SyntaxHighlight


{
public void configure(Properties config);
public StringBuffer transform(StringBuffer input);
}

The transform() method is where the actual syntax highlighting occurs.

2. The syntax highlight module is configurable through information contained in a


Properties object. The keys in this object refer to an aspect of the programming language
to be highlighted (e.g. keyword, operator, comment, etc.). The value part is the color code for
the corresponding key item in hexadecimal format (RRGGBB).

3. The output buffer when rendered in a browser must display the text using fixed-width font
(e.g. Courier New). Indentations, spaces and line breaks of the input buffer must be
preserved.

4. In addition to colored highlight, you might also want to use additional font effects like bold
and italics.

5. Take a look at the functionality of the java.io.StreamTokenizer class.

Training Assignments Page 7 of 27


MindTree C2 | Assignments

Problem 3 Queue Monitor


Problem ID: queuemon

Description:

A queue is an ordered collection of elements that implements a FIFO (first-in first-out) policy for
insertion and retrieval of elements in the collection. Items are inserted into the queue (this
operation is called a push). Subsequently, items are retrieved from the queue (this operation is
called a pop) in the same order as they were inserted into the queue.

A queue monitor defines an asynchronous boundary in an application process flow. A queue


implementation is used to define the boundary, with one application thread inserting items into
the queue and another thread processing the items as soon as they are inserted into the queue.

The queue monitor should be implemented as a generic, reusable component. It should be able to
use any queue implementation (subject to defined specifications). The monitor implements the
thread that is used to retrieve and process items from the queue. Following the principles of
inversion of control, the monitor uses and invokes a delegate to perform the actual processing of
queued items. The monitor implementation defines the contract for the delegate.

The specific queue implementation and delegate instance is associated with the monitored queue
as part of the monitor setup. There can be only one instance of a delegate per monitor instance.
Delegates and queues, once set on a monitor, cannot be replaced during the lifetime of the
monitor. A delegate performs its processing operation on the same thread that is used to retrieve
the corresponding item from the queue.

Instructions:

1. Specify a queue interface called IMonitoredQueue. This defines the contract that any queue
implementation must satisfy for the same to be associated with a queue monitor. The API
signature for the queue is as follows:

public interface IMonitoredQueue


{
public void push(Object item);
public Object pop();
public Object peek();
public int size();
}

The push() method is used to add an item at the end of the queue. The pop() method is
used to remove an item from the top of the queue. The peek() method is used to obtain the
topmost item on the queue without removal. The size() method returns a count of the
number of items currently in the queue.

Training Assignments Page 8 of 27


MindTree C2 | Assignments

2. Specify a delegate interface called IMonitorDelegate. This defines the contract for a
delegate that is associated with the monitor for processing queued items. The API signature
for the delegate is as follows:

public interface IMonitorDelegate


{
public void processItem(Object item);
}

3. The class implementing the queue monitor must be called QueueMonitor. The public
signature for this class should include the following:

public class QueueMonitor


{
public void setQueue(IMonitoredQueue queue);
public void registerDelegate(IQueueDelegate delegate);
public void push(Object item);
public void start();
public void stop();
}

The push() method is used to push an item into the queue. The start() and stop()
methods are used to respectively initiate and terminate the monitoring and processing
operations.

4. The thread that is used to retrieve the item from the queue should be implemented as an
inner class to the QueueMonitor class. You must name this class as MonitorThread. To
keep the delay between insertion and deletion at a minimum, the monitor thread should not
poll the queue. Instead, cause the monitor thread to wait till one item is pushed onto the
queue. Notify the waiting thread to start its processing from within the push() method after
item insertion.

5. If a delegate is not registered with the monitor, items inserted are immediately removed
without further processing.

6. Handle clean termination at the time of monitor stop. Threads should not be left running
after the monitor is stopped, that might potentially block application termination or steal
CPU cycles.

7. The stop() method on the monitor should not wait till the queue is empty or till completion
of any running processing operations.

8. Perform appropriate exception handling so that the MonitorThread does not terminate due
to any error encountered during item processing by the delegate.

Training Assignments Page 9 of 27


MindTree C2 | Assignments

Problem 4 Priority Queue


Problem ID: pqueue

Dependencies:

Problem #3: Queue monitor implementation.

Description:

A priority queue provides a partial deviation of the standard FIFO policy of queue
implementation. Items are added to the queue with an assigned priority. During retrieval, item
with the highest priority is always returned and removed. If two items have the same priority,
they are retrieved in the same order as they were inserted in the queue.

Implement a priority queue that allows 3 levels of priority for queued items: LOW, NORMAL
and HIGH. Also write suitable adapter classes that will allow you to associate this queue with the
queue monitor functionality.

Instructions:

1. The priority queue class must be called PriorityQueue. The public signature of this class is
as follows:

public class PriorityQueue


{
public void push(Object item, int priority);
public Object pop();
public Object peek();
public int size();
}

2. Define all priority values as integer constants in a separate interface called


IPriorityConstants.

3. Do not implement any of the interfaces from the queue monitor problem while defining your
priority queue.

Training Assignments Page 10 of 27


MindTree C2 | Assignments

Problem 5 Thread Pool


Problem ID: thrdpool

Description:

Many Java applications make use of multi-threading and associated concurrency to execute
multiple tasks in parallel. In addition concurrency can be used to breakup large, iterative tasks
into smaller tasks and execute the latter in parallel. This helps improve performance.

For applications that are strongly concurrent, large number of threads get created and destroyed.
In an uncontrolled environment, this poses two problems:

• Thread creation takes a visible amount of time, especially when the number of threads is
large and the concurrent processes are heavy on CPU and memory.

• Lack of control in the number of threads created and improper cleanup can cause memory
leaks.

A thread pool helps to address these issues by providing a controlled environment for thread
creation, destruction and reuse.

A thread pool is a generic system that internally maintains a pool of running threads. At any
point in time, some of the threads can be idle (wait state); others will be active in processing a
concurrent operation. The actual concurrent operation to be performed is defined as a realization
of the Runnable interface. Threads that are idle are said to be in the free pool. Threads that are
currently executing are said to be in the used pool.

Executing a concurrent operation using a thread pool involves the following steps:

1. The concurrent operation must realize the Runnable interface. The corresponding run()
method constitutes the main body of the operation. For our convenience we will call such an
implementation, a task.

2. The task is added to the thread pool for execution. This operation blocks till at least one
thread in the pool enters the idle state.

3. A thread from the idle pool is blocked for execution. The task is registered with the thread
and the thread is activated. The activated thread executes the run() method of the registered
task.

4. On completion of task execution, the task is deregistered from the thread. The state of the
thread is changed to idle and the status marked as unblocked for another execution.

Training Assignments Page 11 of 27


MindTree C2 | Assignments

Instructions:

1. The class implementing the thread pool must be called ThreadPool. The API signature for
this class is as follows:

public class ThreadPool


{
public ThreadPool(int minThread, in maxThread);
public ThreadPool(int minThread, in maxThread, long idleTime);
public void execute(Runnable task);
public void shutdown(long timeout);
}

Internal threads used by the pool must derive the standard thread class and must be called
PooledThread.

2. At the time of creation, the thread pool is configured with the following parameters:

• minThread: This specifies the minimum number of threads that are always available
within the pool.

• maxThread: The maximum number of threads that can be allowed to exist in the pool.
This must be greater than or equal to the minimum thread value.

• idleTime: If the number of threads in the pool is more than the minimum allowed (as can
happen after a heavy application load), the extra idle threads must be stopped and
removed from the pool over a period of time. The idle time specified the duration for
which a thread must be idle after which it is automatically removed (garbage collected).

3. Pool termination (via. the shutdown() method) must wait for the specified amount of time
to allow all threads to become idle. New tasks cannot be added to the pool once termination
has been initiated. If all threads become idle before the shutdown timeout period, the pool
terminates immediately (does not wait for the shutdown to timeout). Post timeout, all
executing tasks are forced to terminate.

4. Perform appropriate exception handling so that the pooled threads do not terminate
unexpectedly due to any errors encountered during task execution.

Training Assignments Page 12 of 27


MindTree C2 | Assignments

Problem 6 Find Pattern in Files


Problem ID: patfind

Dependencies:

Problem #5: Thread pool implementation.

Description:

Write an application that searches a set of input files for lines containing a match to a given
pattern. When it finds a match in a line, it prints the line to standard output along with the
filename and the line number. A sample output is as provided below:

Searching for: List

PGWHOLE.TXT(3): This list has been downloaded from:


PGWHOLE.TXT(14): This list serves as reference only.
PGWHOLE.TXT(6679): Aug 1991 [feder12x.xxx]18 The Federalist Papers

Found 3 occurrence(s) in 1 file(s)

Instructions:

1. The main class for the application must be called PatternFind. As argument to the
application, you can pass one file name or a directory name as input set of files on which to
perform the search operation. If a directory is specified, all sub directories must be recursed
to build the list of input files.

2. You must use the regular expression parsing rules and implementation as provided by the
java.util.regex package.

3. You must use the thread pool implementation to perform the search on multiple files in
parallel.

4. Your application should perform optimally both for large number of input file sets and large
file sizes.

Training Assignments Page 13 of 27


MindTree C2 | Assignments

Problem 7 Multi-value Properties


Problem ID: mvp

Description:

The standard Properties class in java.util package allows you to store only one value
against a given key. Attempting to set another value on the same overwrite the old value. To
overcome this limitation, write a multi-value properties data structure that allows you to store
multiple values against a given key.

Instructions:

1. The name of the class implementing the data structure must be called
MultiValueProperties. The API signature of this class is as provided below:

public class MultiValueProperties


{
public void load(InputStream input);
public void store(OutputStream output, String header);
public String[] getProperties(String key);
public String getProperty(String key);
public void setProperty(String key, String value);
public void addProperty(String key, String value);
public Enumeration propertyNames();
public Boolean containsKey(String key);
public String[] remove(String key);
public int size();
}

The load() and store() methods must follow the same data interpretation and
transformation logic as implemented in the standard Properties class.

The getProperty() method returns the first value that is mapped against a given key. The
getProperties() method returns all the values mapped against the same key.

The addProperty() method adds a new item to the list of values against a key. This value
is added to the end of the list. The setProperty() method removes all items from the list
of values against a key. The new item is then added to the list of values against the key.

The remove() method removes the given key from the properties object. This method
returns the list of values already mapped against the given key.

The size() method returns the number of unique keys contained in this data structure.

2. Do not extend from any of the standard classes like Properties, HashTable or HashMap
as defined in the java.util package.

Training Assignments Page 14 of 27


MindTree C2 | Assignments

Problem 8 Simple Encryption / Decryption


Problem ID: xorcrypt

Description:

The XOR binary operation can be used to implement a simple symmetric encryption mechanism.
Specifically if A is the original information, B is an encryption key and C is the encrypted data,
then

C = A XOR B
C XOR B = A

For our encryption purposes, we will assume an input data buffer as an array of bytes and arrive
at the encrypted data buffer by performing an XOR operation on a byte by byte basis. The
encryption key used to perform this operation will be a byte array of size 8. This key is repeated
across the input data in intervals of 8 bytes so that the nth byte in the input buffer is encrypted
using the n modulo 8 byte in the encryption key (n starts from zero).

The structure of the output buffer includes the encryption key occupying the first 8 bytes of
output, followed by the encrypted byte array. Encryption should not add more than 8 bytes to the
size of the data buffer.

Implement the encryption and decryption logic as an extension of the FilterOutputStream


and FilterInputStream classes of the java.io package.

Instructions:

1. Your encrypting output stream class must be called XOROutputStream. The corresponding
decrypting input stream class must be called XORInputStream. You may extend these
classes from those extend the FilterOutputStream and FilterInputStream classes.

2. The encryption key should be generated at runtime using random number generation. Key
generation is internal to the encryption routine and should not be provided as an parameter.

3. The encryption and decryption classes must correctly implement all the methods as defined
in the InputStream and OutputStream base classes.

Training Assignments Page 15 of 27


MindTree C2 | Assignments

Problem 9 Socket Server


Problem ID: sockserver

Dependencies:

Problem #5: Thread pool implementation.

Description:

Write a simple concurrent socket server that accepts connections on a TCP port, processes
requests and sends back a response. The socket server should be implemented as a generic,
reusable system that can be adapted for any network protocol. The socket server itself should not
implement any particular protocol. Using the principles of inversion of control, the socket server
should use a delegate to process the network connection and create a response. The socket server
implementation defines the contract for the delegate.

The specific delegate implementation is associated with the socket server during startup. There
can be only one instance of a delegate per server instance. Delegate, once set on a server, cannot
be replaced during the lifetime of the server.

For multi-threaded operation, you must use the thread pool implementation to control the
number of threads created and their lifecycle.

Instructions:

1. Specify a delegate interface called IConnectionHandler. This defines the contract for a
delegate that is associated with the server for processing network requests. The API signature
for the delegate is as follows:

public interface IConnectionHandler


{
public void processRequest(Socket socket);
}

2. The class implementing the socket server must be called SimpleServer. The public
signature of the class should include the following:

public class SimpleServer


{
public void configure(int port, int minThread, int maxThread);
public void setHandler(IConnectionHandler handler);
public void startServer();
public void stopServer();
}

Training Assignments Page 16 of 27


MindTree C2 | Assignments

Problem 10 Simple File Transfer


Problem ID: fileserver

Dependencies:

Problem #8: Simple encryption/decryption.


Problem #9: Socket server.

Description:

Use the concurrent socket server to implement a simple file transfer program. Using this program
you can both download and upload files between a client and a server. You need to implement
both the client and server applications.

For a file download request, the client should open a connection with the server and send out the
following command:

GET filename <CRLF>

Where filename is substituted by the actual name of the file to download and <CRLF> is the
standard carriage-return line-feed line terminating sequence. If the file does not exist on the
remote machine, the server responds with

-ERR <CRLF>

and closes the connection immediately. For a successful download, the server responds with

+OK datasize <CRLF>


__Actual file data as a byte stream__

and closes the connection. The datasize value returns the size of the byte stream following the
response header.

For an upload request, the client should open a connection with the server and send out the
following command:

PUT filename datasize <CRLF>


__Actual file data as a byte stream__

Where filename is substituted by the actual name of the file to download. The datasize value
returns the size of the byte stream following the request header.

The server will respond with either of +OK <CRLF> to indicate successful transfer, or -ERR
<CRLF> to indicate failure of the upload operation and close the connection.

Training Assignments Page 17 of 27


MindTree C2 | Assignments

Instructions:

1. The main class for the client application must be called FileTransferClient. The
corresponding socket server delegate must be called FileTransferHandler.

2. The file transfer handler can only serve files from or upload files to a specific directory on the
remote machine. The name of the directory is provided as a configuration to the transfer
handler.

3. The data stream following the request and response header during uploads and downloads
must be encrypted using the XOR encryption method.

4. The data stream should also be compressed to reduce the volume of network traffic. Use
appropriate classes from the java.util.zip package for compression and inflation of file
data.

Training Assignments Page 18 of 27


MindTree C2 | Assignments

Problem 11 Bean to Map


Problem ID: beanmap

Description:

Write a data transformer to translate data from a Java bean object to a HashMap. The
transformation should be bi-directional: it should be possible to translate the same data from a
HashMap to a Java bean object.

Use reflection to obtain the properties of a Java bean object. The properties should be structured
as key-value pairs. For example given the class:

public class SampleBean


{
public void setName(String s);
public String getName();
public void setAge(int age);
public int getAge();
}

the bean properties would include the keys: name and age with their corresponding values.

Once you are able to view the bean information as a properties set, enumerative through the
same to populate a HashMap. For the reverse operation, enumerate through the HashMap keys
and use key matching to populate values from the map to the bean.

Instructions:

1. The transformer class must be called BeanMapTransformer. The public API of this class
must include the following:

public class BeanMapTransformer


{
public void beanToMap(Object bean, HashMap map);
public void mapToBean(HashMap map, Object bean);
}

2. For bean properties whose values are primitives, the entries in the HashMap should wrap the
same in the Java object representation for the corresponding primitive type. For example an
int value is represented as an Integer object in the HashMap. Unwrap the same from
object representation to primitives for the reverse transformation.

3. For a bean property, it the value is an array of objects or primitives, wrap the same in a
suitable Collection object. Unwrap the same from the Collection object to the array
during reverse transformation.

Training Assignments Page 19 of 27


MindTree C2 | Assignments

4. For a bean property, if the value is an object that is not a Java object representation for
primitives, check if the object is a Java bean and generate the map representation of the same.
An object is assumed to be a bean if it has at least one getter and setter method pair that can
translate to a property information. Objects that fail this check should be stored in the same
format in the HashMap. Perform this transformation recursively through the entire object
containment hierarchy.

5. During transformation from the HashMap to the bean object, you need to handle
Collection and Map type objects specially, since they can represent either an
untransformed value or a transformed bean object value.

Training Assignments Page 20 of 27


MindTree C2 | Assignments

Problem 12 Classpath expansion


Problem ID: xploader

Description:

Large applications typically rely on numerous libraries (ZIP and JAR files) that are not a part of
the standard Java distribution. For a large number of external libraries, setting up the classpath
for the application can become a problem as the classpath argument to the JVM tend to become
exceptionally long. This configuration is also error prone, as the probability to miss out entries in
the classpath increases with a large number of external libraries.

Write a custom class loader that can be used to load classes from multiple external ZIP and JAR
files that are not on the classpath. For better usability, all ZIP and JAR files are assumed to be
stored under one directory in the file system. The custom class loader will enumerate this
directory location for library files and access them directly during individual class loading.

Create a generic launcher application that will expand the class path using our custom loader.
The launcher will in turn invoke the main() method of a custom application that relies on
libraries in the expanded classpath.

Instructions:

1. The class implementing the class loader must be named ExtensionClassLoader. The public
API of this class must include the following:

public class ExtensionClassLoader


{
public void setLibPath(String path);
protected Class findClass(String name)
throws ClassNotFoundException;
protected URL findResource(String name);
}

The method setLibPath() is used to specify the location of the directory containing
additional application libraries. This information must be provided before attempting to load
any class using this class loader.

2. The main class for the generic launcher must be called Launcher. The directory path
containing application libraries and the name of the main application class are provided as
command line arguments.

3. Since the launcher is generic, it must invoke the main() method of the target application
using reflection. It should also register the expansion class loader as the system class loader
for the JVM.

Training Assignments Page 21 of 27


MindTree C2 | Assignments

Problem 13 Class file disassembler


Problem ID: deasm

Dependencies:

Problem #2: Syntax highlight.


Problem #12: Classpath expansion.

Description:

Write a simple class file disassembler to generate the signature of any Java class or interface. A
sample output is as provided below for the purpose of illustration:

public final class java.lang.Boolean extends java.lang.Object


implements java.io.Serializable
{ public static final java.lang.Boolean TRUE;
public static final java.lang.Boolean FALSE;
public static final java.lang.Class TYPE;
public boolean booleanValue();
public java.lang.Boolean(boolean);
public static java.lang.Boolean valueOf(java.lang.String);
}

The application should be able to save the output in a variety of formats and locations depending
on the command line arguments provided:

• Print to the console (this is the default behavior).


• Save to a file in plain text format.
• Save to a file in HTML format with syntax highlight.

The application should have the capability to load information from compiled class files in the
file system, ZIP and JAR archives that are not in the JVM classpath.

Instructions:

1. Use Java reflection to obtain information about the inheritance hierarchy and all class
members.

2. Modify the expansion class loader suitably to load classes from the file system paths, ZIP and
JAR archives that are not in the JVM classpath.

3. The output location (console, file, etc.), the output format (text, html, etc.) and the external
classpath location (file system path, path to folder containing ZIP and JAR archives) must be
provided as command line arguments to the application.

Training Assignments Page 22 of 27


MindTree C2 | Assignments

Problem 14 Object Cache


Problem ID: lrucache

Description:

An object cache represents a temprorary storage for frequently accessed objects in memory. An
object is stored in cache against a unique key name. The key name is used to identify and refer to
the object during retrieval from cache. Older objects are removed from cache to make place for
newer ones (the cache size is fixed).

Write a generic cache implementation which has a maximum size and uses a Least Recently Used
(LRU) algorithm to remove items from the cache when the maximum size is reached and new
items are added. The cache should be generic to store any kind of objects.

Instructions:

1. The class implementing the cache must be called LRUMap. The class must have the following
API signature:

public class LRUMap


{
public void setMaximumSize(int size);
public Object get(Object key);
public void put(Object key, Object value);
}

The method setMaximumSize() is used to limit the size of the cache to contain a maximum
of n items at a time.

2. Every time an object is retrieved via. the get() method, it is marked as the last used object,
so that it is the last item to be removed from the cache using the LRU algorithm. Note that the
get() method does not remove the object from cache.

3. Whenever an object is added to the cache via. the put() method, it is marked as the last used
object, so that it is the last item to be removed from the cache using the LRU algorithm.

Training Assignments Page 23 of 27


MindTree C2 | Assignments

Problem 15 TCP Snoop


Problem ID: tcpsnoop

Dependencies:

Problem #9: Socket Server.

Description:

While writing client-server applications, a good debugging aid is the ability to monitor and see
the actual information being transferred between the client and the server. You need to write a
TCP snooping application for the same.

The snoop application will act as a proxy between the client and the server. The application will
start a simple socket server for the client to connect. For each client connection, the application
will open a corresponding socket connection with the target server. The application will transmit
the client request to the server and transmit the server response back to the client. Request and
response data are transmitted without modification or data loss. The snoop application saves the
request and response data into separate files on disk.

Instructions:

1. The main class for the snoop application must be named TcpSnoop. The public API for the
class should have the following signature:

public class TcpSnoop


{
public void configure(Properties config);
public void startServer();
public void stopServer();
}

The configure() method takes a Properties object containing configuration information as


name-value pairs. You must load this information from disk. The information in the
Properties object must have the keys with the following information:

• localPort: port number on which to start the socket server for client access.

• remoteHost: host address of the remote machine on which the server application is
active.

• remotePort: port number to which the server application is bound.

• inputFile: file name under which the data from the client is stored.

Training Assignments Page 24 of 27


MindTree C2 | Assignments

• outputFile: file name under which the data from the server is stored.

• maxConnections: the maximum number of concurrent client connections that are


allowed.

2. The delay in client-server communication, introduced by the snoop application, must be kept
a minimum.

3. The snoop application must perform appropriate socket closure and cleanup if the server or
the client application closes connection. The application should make no assumptions about
when the server or the client should close a connection.

4. The snoop application should be generic to work with any client-server system and any
protocol.

5. Explore possibilities to view the input and output data in real-time.


Hint: Have the snoop application setup additional telnet servers -- one for the input buffer
and one for the output buffer. Use a telnet client to connect and observe the data flow.

Training Assignments Page 25 of 27


MindTree C2 | Assignments

Problem 16 JDBC Connection Pool


Problem ID: dbpool

Description:

For database-centric applications, connectivity with the database becomes a performance


bottleneck. The problem tends to become more acute with the number of connections being
opened and the frequency at which connections are opened and closed. A simple solution is to
maintain open connections with the database and reuse connections across multiple database
calls.

Write a generic JDBC connection pool that maintains and reuses open connections with the
database across multiple application calls. The connection pool should be capable of working
with any database implementation that exposes a Java connectivity interface in accordance with
the standard JDBC specification.

As part of the implementation you must define three separate components: a connection pool, a
JDBC driver and a connection.

The connection pool will create and maintain connections using an underlying JDBC driver
implementation for the target database. At startup time, the pool should be configured with both
database parameters (e.g. the driver URL, authentication information, transaction isolation level,
auto commit flag, etc.) and pool parameters (minimum and maximum open connections).

The JDBC driver must implement the Driver interface as defined in the java.sql package. The
driver interacts with the connection pool to obtain a connection. The driver automatically
registers itself with the JDBC DriverManager when the class is loaded by the VM. The public
API of the driver must include the following:

public class PoolDriver implements Driver


{
public static configure(Properties config);
}

Note: you also need to implement all other methods that are mandated by the Driver interface.

The driver internally contains the connection pool. There can be only one pool instance per driver
instance. The configure() method must be used to configure the pool after the driver is loaded
and registered and prior to any database invocation.

On request for new connections, the connection pool must return an instance of pooled
connection that implements the Connection interface as defined in the java.sql package. This
connection is a thin wrapper over another connection instance and provided by the underlying
JDBC driver for the target database. Invoking a method on the pooled connection will directly
execute the corresponding method in the underlying connection.

Training Assignments Page 26 of 27


MindTree C2 | Assignments

The application will use the pooled a connection just like any other “normal” connection. The
only difference is that a call to the close() method does not close the connection. Instead, the
connection is marked as available and put back into the pool.

Instructions:

1. The classes implementing the pool, the driver and the connection must be named
ConnectionPool, PoolDriver and PoolConnection respectively.

2. The Properties object used to configure the connection pool must have the following
information structure:

• jdbc.driver: The URL for the underlying JDBC driver.

• jdbc.user: user name part of the authentication information.

• jdbc:password: password part of the authentication information.

• jdbc.autocommit: the auto commit information globally set for all connections.

• jdbc.readonly: the read-only information globally set for all connections.

• pool.minSize: the initial size of the pool, also the minimum number of connections
that are always open with the database.

• pool.maxSize: the maximum size that the pool can grow to.

• pool.timeout: the timeout interval in milliseconds after which an idle connection gets
closed and removed from the pool.

3. When asked for a connection, the driver should block if the maximum size of the pool is
reached and no connections are free.

4. Free connections that are not in use must be automatically closed after a specified duration of
time. All connections must be closed when the driver is de-registered with the
DriverManager.

5. When a connection is put back into the pool, the following changes are made to the
underlying connection:

• If autocommit was set to true, commit() is called.

• clearWarnings() is called.

• The catalog, transaction isolation level, auto commit flag and read-only flag are set back
to their original values (only if the values were changed).

Training Assignments Page 27 of 27

Вам также может понравиться