Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment this week is to write a utility class which will provide the fol

ID: 3664064 • Letter: Y

Question

Your assignment this week is to write a utility class which will provide the following four log methods that print information to the console:

public void logDebug(String strMessage)

public void logInfo(String strMessage)

public void logWarning(String strMessage)

public void logError(String strMessage)

Each of these methods will accept a String argument that will contain a useful message and print the message to the output window. In addition, you should have methods that allow the user to turn logging on and off:

public void enableLogging()

public void disableLogging()

Finally, you need a way to set the current log level. This level will determine what information gets logged.

public void setLogLevel(int logLevel) // log levels will be numeric values (1-error, 2- warning, 3-informational, 4-debug)

Here is a table that shows what will be printed depending on the log level set:

Output:

Debug
Info
Warning
Error

Output:

Info
Warning
Error

Output:

Warning
Error

Output:

Error

Notice that as the level approaches "Error" less information is actually logged. This allows you to control the detail of your logger output.

In order to create this class you will add a package to your project to contain the Logger class. You should name this package, "utilities". It should contain one class, "Logger.java", that contains all of the methods listed above.

Below is a test class that you can download to test your class.

If your class is written correctly, you should see the following output when you run the test class.

Testing disable/enable logging
You should see no messages:
Testing Debug Detail
You should see four messages:
DEBUG: Message #1
INFO: Message #2
WARNING: Message #3
ERROR: Message #4
Testing Info Detail
You should see three messages:
INFO: Message #1
WARNING: Message #2
ERROR: Message #3
Testing Warning Detail
You should see two messages:
WARNING: Message #1
ERROR: Message #2
Testing Error Detail
You should see one messages:
ERROR: Message #1

Test class to test logger: TestLogger.java

/*
* Test application to test using the Logger class
*/
package utilities;

public class TestLogger {
  
   public static void main(String[] args) {
      
       // Create a Logger object
       Logger myLogger = new Logger();
     
        // Test #1 - Test disable/enable logging
        System.out.println("Testing disable/enable logging");
        System.out.println("You should see no messages:");
        myLogger.setLogLevel(4);
        myLogger.disableLogging();
        myLogger.logDebug("Test failed, this should not print!");
        myLogger.logInfo("Test failed, this should not print!");
        myLogger.logWarning("Test failed, this should not print!");
        myLogger.logError("Test failed, this should not print!");

        myLogger.enableLogging();
         
        // Test #2 - Test Debug Detail
        System.out.println("Testing Debug Detail");
        System.out.println("You should see four messages:");
        myLogger.setLogLevel(4);
        myLogger.logDebug("Message #1");
        myLogger.logInfo("Message #2");
        myLogger.logWarning("Message #3");
        myLogger.logError("Message #4");
      
        // Test #3 - Test Info Detail
        System.out.println("Testing Info Detail");
        System.out.println("You should see three messages:");
        myLogger.setLogLevel(3);
        myLogger.logDebug("Test failed, this should not print!");
        myLogger.logInfo("Message #1");
        myLogger.logWarning("Message #2");
        myLogger.logError("Message #3");
      
        // Test #4 - Test Warning Detail
        System.out.println("Testing Warning Detail");
        System.out.println("You should see two messages:");
        myLogger.setLogLevel(2);
        myLogger.logDebug("Test failed, this should not print!");
        myLogger.logInfo("Test failed, this should not print!");
        myLogger.logWarning("Message #1");
        myLogger.logError("Message #2");
      
        // Test #5 - Test Error Detail
        System.out.println("Testing Error Detail");
        System.out.println("You should see one message:");
        myLogger.setLogLevel(1);
        myLogger.logDebug("Test failed, this should not print!");
        myLogger.logInfo("Test failed, this should not print!");
        myLogger.logWarning("Test failed, this should not print!");
        myLogger.logError("Message #1");
        // ----------------------------------------------------------------

    }
}  

Debug

Output:

Debug
Info
Warning
Error

Info

Output:

Info
Warning
Error

Warning

Output:

Warning
Error

Error

Output:

Error

Explanation / Answer

o start off, we’ll create a basic WCF web service in Visual Studio by choosing the WCF Web Service template to begin a new project. The template will automatically generate a WCF svc class and a service contract interface. To keep things simple, we’ll implement a single method, called SayHelloWorld(), which will accept a composite type of HelloWorldType and return a string. The composite type will allow you to specify a name and a language. The WCF service contract interface appears as follows:

As you can see in the above code, our contract is fairly simple. We have a single method for performing SayHelloWorld() and a custom type to help make our SOAP XML a little more interesting. With the interface service contract defined, we can customize the main WCF class as follows:

In the code above, we simply examine the HelloWorldType and return a string based upon the name and language. With the initial code setup, we’ll now implement the plaintext username and password authentication.

PlainText Username and Password Authentication in a WCF Web Service

Normally, Windows expects all WCF services to use a security certificate and encrypted password for authentication. While this is certainly the preferred method for communicating over the un-trusted Internet, it is not always viable, especially in development environments or settings where other forms of security may be used.

WCF Authentication with a Plaintext Password

In our example WCF service, we’ll implement a plaintext username and password validation using a custom binding, named ClearUsernameBinding. We’ll actually configure the ClearUsernameBinding within the web.config for the WCF service (and the WCF client). As with any custom WCF authentication, we’ll need to provide a CustomUserNameValidator, as shown below:

In the above code, we have a very basic CustomUserNameValidator, which simply verifies the username and password against hard-coded values. You can easily enhance this to validate against a database or other authentication structures.

To use the plaintext username and password validation, you’ll also need to download and add a reference to ClearUsernameBinding.dll in your project.

With the authentication complete, we can move on to configuring the WCF service’s web.config settings.

WCF Configuration Flexibility Comes With a Price

Often, the trickiest part of developing a WCF web service is configuring the web.config settings. For our example, we’ll be adding a custom binding for the plaintext username and password authentication. We’ll also be adding a custom endpoint and service block, along with a custom behavior for usernameauthentication.

  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  31  32  33  34  35  36  37  38  39  
  namespace WcfService1  {      [ServiceContract(Name="HelloWorldService")]      public interface IHelloWorldService      {          [OperationContract]          string SayHelloWorld(HelloWorldType HelloWorldType);      }        [DataContract]      public enum LanguageType      {          [EnumMember]          English,          [EnumMember]          Spanish      };        [DataContract]      public class HelloWorldType      {          LanguageType _language = LanguageType.English;          string _name;            [DataMember]          public LanguageType Language          {              get { return _language; }              set { _language = value; }          }            [DataMember]          public string Name          {              get { return _name; }              set { _name = value; }          }      }  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote