What is a wrapper in programming?
In a software context, the term “wrapper” refers to programs or codes that literally wrap around other program components. Several different wrapper functions can be distinguished. They are often used for ensuring compatibility or interoperability between different software structures. Alternatively, they can be used for visual reasons, as is the case with HTML or CSS wrappers. Wrappers can be individual software components, independent software products, software architectures, classes in object-oriented programming, or frameworks.
If you want to use functions or code blocks of another programming language within a program, you can encapsulate them using a wrapper. The main program communicates exclusively with the wrapper, which forwards the commands to the wrapped program and returns the results. The wrapper itself is the only component that communicates directly with both parts of the program.
Wrappers can be used in a variety of ways in programming and software development. The following examples show how wrappers work and the different tasks they perform.
Wrappers as translators of user input
Forms in programs or web applications expect input that the program can process. Programs developed in many European countries expect numbers to be entered with a comma instead of a decimal point and dimensions to be entered in meters and centimeters. When you use these program components in your own applications, it’s not always possible to adapt them to the default user inputs in the US. This invariably leads to incorrect results or even program errors.
A wrapper can solve this problem. The input form forwards the input directly to the wrapper instead of directly to the external program. The wrapper analyzes the inputs and translates them into valid inputs for the external program without having to modify the program.
Wrappers for database access
Databases from different vendors usually cannot be used together because their data tables, queries, or query languages are not compatible with each other. Here too, a wrapper can be the solution. As with any type of wrapper, the idea is to detect inconsistencies between different software interfaces and use the wrapper to bridge the gap.
Java Database Connectivity (JDBC), a database interface by Oracle, is a typical example of a wrapper. In its wrapper function, JDBC gives access to different relational databases. JDBC connects individual databases using special drivers. SQL queries are directed exclusively to JDBC, not the databases. JDBC converts queries into the database query language, returning Java-compatible results. Accordingly, the requesting program always receives data in a uniform format, regardless of the database used.
Wrappers in object-oriented programming
Object-oriented programming uses different structural patterns that basically always work in the same way regardless of the programming language used. The Adapter and Decorator design patterns are structural patterns and are also called wrappers.
An adapter conceals incompatible interfaces between individual classes. By translating one interface into another, an adapter allows the classes to communicate with each other. This is especially important if you want to use classes or entire class libraries in new projects. These libraries use unique, standardized interfaces that may not be changed because they have to be valid for a large number of programs. The wrapper – in this case the adapter – is the crucial link in the communication.
A decorator enables functions to be added to a class without changing the class itself. To the calling program object, the decorator has the same interface as the original class. In this way, nothing needs to be changed in the calling object. As the wrapper, the decorator passes the calls on to the class. The decorator directly handles new functions that are not included in the class. It returns the results in such a way that they appear to the calling object like results of the decorated class.
Wrappers for designing HTML documents
Wrappers are often used for (re)designing websites in HTML and CSS. Without wrappers, you would have to change multiple stylesheets and make sure they fit together again after making individual adjustments such as changes to the page margins of a browser window.
An easier approach is to use a DIV container as a wrapper to hold the entire content of the page, as in the following wrapper example:
<html>
<head>
...
</head>
<body>
<div class="wrapper">
…
</div>
</body>
</html>
The actual content of the page is then enclosed inside this wrapper container.
The wrapper is defined as a stylesheet in the associated CSS file:
body {
margin: 0;
padding: 0
}
.wrapper {
width: 500px;
margin: 25px auto;
}
In this example of a wrapper, the container is assigned a width of 500 pixels via the width parameter. The margins at the top and bottom are defined using the margin parameter, which is set to 25 pixels. The left and right margins are automatically derived from the width of the browser window and the width of the container.
By simply changing the wrapper, you can easily adjust the page margins without making further changes to the HTML or CSS code.
TCP wrappers in Linux systems
The inetd background service in Linux and other operating systems based on UNIX run as TCP wrapper. inetd listens to network sockets and accepts connection requests. A configuration file specifies which ports to listen to. The requests are evaluated and the inetd service specified in the configuration file is started for the corresponding port. In most cases, these programs are daemons that run in the background.
If the connection is ended, inetd automatically stops the service. Starting services on demand considerably reduces the load on system resources compared to automatically starting network services that may not be required at all. inetd functions as a wrapper that receives network requests from all programs without communicating directly with the individual services.
TCP wrappers can also be used to prevent unwanted access from a network. The TCP wrapper is queried by inetd or special server software. Allowed and denied hosts and computers are entered in the files /etc/hosts.allow and /etc/hosts.deny.